Total Complexity | 8 |
Total Lines | 61 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php namespace GeneaLabs\LaravelModelCaching\Console\Commands; |
||
6 | class Flush extends Command |
||
7 | { |
||
8 | protected $signature = 'modelCache:flush {--model=}'; |
||
9 | protected $description = 'Flush cache for a given model. If no model is given, entire model-cache is flushed.'; |
||
10 | |||
11 | public function handle() |
||
12 | { |
||
13 | $option = $this->option('model'); |
||
14 | |||
15 | if (! $option) { |
||
16 | return $this->flushEntireCache(); |
||
17 | } |
||
18 | |||
19 | return $this->flushModelCache($option); |
||
20 | } |
||
21 | |||
22 | protected function flushEntireCache() : int |
||
31 | } |
||
32 | |||
33 | protected function flushModelCache(string $option) : int |
||
34 | { |
||
35 | $model = new $option; |
||
36 | $usesCachableTrait = $this->getAllTraitsUsedByClass($option) |
||
37 | ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable"); |
||
38 | |||
39 | if (! $usesCachableTrait) { |
||
40 | $this->error("'{$option}' is not an instance of CachedModel."); |
||
41 | $this->line("Only CachedModel instances can be flushed."); |
||
42 | |||
43 | return 1; |
||
44 | } |
||
45 | |||
46 | $model->flushCache(); |
||
47 | $this->info("✔︎ Cache for model '{$option}' has been flushed."); |
||
48 | |||
49 | return 0; |
||
50 | } |
||
51 | |||
52 | protected function getAllTraitsUsedByClass(string $classname, bool $autoload = true) : Collection |
||
67 | } |
||
68 | } |
||
69 |