Passed
Pull Request — master (#223)
by
unknown
05:06
created

Clear::getAllTraitsUsedByClass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Caching;
4
use Illuminate\Console\Command;
5
6
class Clear extends Command
7
{
8
    protected $signature = 'modelCache:clear {--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
23
    {
24
        app('cache')
25
            ->store(config('laravel-model-caching.store'))
26
            ->flush();
27
28
        $this->info("✔︎ Entire model cache has been flushed.");
29
30
        return 0;
31
    }
32
33
    protected function flushModelCache(string $option) : int
34
    {
35
        $model = new $option;
36
        $usesCachableTrait = Caching::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