Passed
Push — master ( 9782a4...be7447 )
by Mike
02:56
created

Flush::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 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.';
9
10
    public function handle()
11
    {
12
        $option = $this->option('model');
13
14
        if (! $option) {
15
            $this->error("You must specify a model to flush a model's cache:");
16
            $this->line("modelCache:flush --model=App\\Model");
17
18
            return 1;
19
        }
20
21
        $model = new $option;
22
        $usesCachableTrait = collect(class_uses($model))
23
            ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
24
25
        if (! $usesCachableTrait) {
26
            $this->error("'{$option}' is not an instance of CachedModel.");
27
            $this->line("Only CachedModel instances can be flushed.");
28
29
            return 1;
30
        }
31
32
        $model->flushCache();
33
        $this->info("✔︎ Cache for model '{$option}' has been flushed.");
34
    }
35
}
36