Passed
Push — master ( f210e5...c7d0c7 )
by Mike
02:17
created

Flush::flushEntireCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
2
3
use Illuminate\Console\Command;
4
5
class Flush extends Command
6
{
7
    protected $signature = 'modelCache:flush {--model=}';
8
    protected $description = 'Flush cache for a given model. If no model is given, entire model-cache is flushed.';
9
10
    public function handle()
11
    {
12
        $option = $this->option('model');
13
14
        if (! $option) {
15
            return $this->flushEntireCache();
16
        }
17
18
        return $this->flushModelCache($option);
19
    }
20
21
    protected function flushEntireCache() : int
22
    {
23
        cache()
24
            ->store(config('laravel-model-caching.store'))
25
            ->flush();
26
27
        $this->info("✔︎ Entire model cache has been flushed.");
28
29
        return 0;
30
    }
31
32
    protected function flushModelCache(string $option) : int
33
    {
34
        $model = new $option;
35
        $usesCachableTrait = collect(class_uses($model))
36
            ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
37
38
        if (! $usesCachableTrait) {
39
            $this->error("'{$option}' is not an instance of CachedModel.");
40
            $this->line("Only CachedModel instances can be flushed.");
41
42
            return 1;
43
        }
44
45
        $model->flushCache();
46
        $this->info("✔︎ Cache for model '{$option}' has been flushed.");
47
48
        return 0;
49
    }
50
}
51