Passed
Pull Request — master (#288)
by Mike
05:55
created

Clear::flushDatabaseCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 26
rs 9.7666
1
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
2
3
use GeneaLabs\LaravelModelCaching\CachedModel;
4
use Illuminate\Console\Command;
5
use Illuminate\Container\Container;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Cache;
8
9
class Clear extends Command
10
{
11
    protected $signature = 'modelCache:clear {--model=} {--database=}';
12
    protected $description = 'Flush cache for a given model. If no model is given, entire model-cache is flushed.';
13
14
    public function handle()
15
    {
16
        $model = $this->option('model');
17
        $database = $this->option('database');
18
19
        if ($model) {
20
            return $this->flushModelCache($model);
21
        }
22
23
        if ($database) {
24
            return $this->flushDatabaseCache($database);
25
        }
26
27
        return $this->flushEntireCache();
28
    }
29
30
    protected function flushEntireCache() : int
31
    {
32
        $config = Container::getInstance()
33
            ->make("config")
34
            ->get('laravel-model-caching.store');
35
36
        Container::getInstance()
37
            ->make("cache")
38
            ->store($config)
39
            ->flush();
40
41
        $this->info("✔︎ Entire model cache has been flushed.");
42
43
        return 0;
44
    }
45
46
    protected function flushDatabaseCache(string $database) : int
47
    {
48
        $useDatabaseKeying = Container::getInstance()
49
            ->make("config")
50
            ->get("laravel-model-caching.use-database-keying");
51
        
52
        if (! $useDatabaseKeying) {
53
            $this->error("Database keying must be enabled in 'config/laravel-model-caching.php'.");
54
            $this->line("No caches were flushed.");
55
56
            return 1;
57
        }
58
59
        // get all tags
60
        $tags = collect()
61
            ->filter(function ($tag) use ($database) {
62
                $parts = explode(":", $tag);
63
64
                return ($parts[3] ?? "") === $database;
65
            })
66
            ->toArray();
67
68
        (new CachedModel)->flushCache($tags);
69
        $this->info("✔︎ Cache for database '{$database}' has been flushed.");
70
71
        return 0;
72
    }
73
74
    protected function flushModelCache(string $modelClass) : int
75
    {
76
        $model = new $modelClass;
77
        $usesCachableTrait = $this->getAllTraitsUsedByClass($modelClass)
78
            ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
79
80
        if (! $usesCachableTrait) {
81
            $this->error("'{$modelClass}' is not an instance of CachedModel.");
82
            $this->line("Only CachedModel instances can be flushed.");
83
84
            return 1;
85
        }
86
87
        $model->flushCache();
88
        $this->info("✔︎ Cache for model '{$modelClass}' has been flushed.");
89
90
        return 0;
91
    }
92
93
    /** @SuppressWarnings(PHPMD.BooleanArgumentFlag) */
94
    protected function getAllTraitsUsedByClass(
95
        string $classname,
96
        bool $autoload = true
97
    ) : Collection {
98
        $traits = collect();
99
100
        if (class_exists($classname, $autoload)) {
101
            $traits = collect(class_uses($classname, $autoload));
102
        }
103
104
        $parentClass = get_parent_class($classname);
105
106
        if ($parentClass) {
107
            $traits = $traits
108
                ->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
109
        }
110
111
        return $traits;
112
    }
113
}
114