Passed
Push — master ( 3a4071...1777bb )
by Mike
03:01
created

Cachable::flushCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use GeneaLabs\LaravelModelCaching\CacheKey;
4
use GeneaLabs\LaravelModelCaching\CacheTags;
5
use GeneaLabs\LaravelModelCaching\CachedModel;
6
use Illuminate\Cache\TaggableStore;
7
use Illuminate\Database\Query\Builder;
8
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
9
use GeneaLabs\LaravelModelCaching\CachedBuilder;
10
11
trait Cachable
12
{
13
    protected $isCachable = true;
14
15
    protected function cache(array $tags = [])
16
    {
17
        $cache = cache();
18
19
        if (config('laravel-model-caching.store')) {
20
            $cache = $cache->store(config('laravel-model-caching.store'));
21
        }
22
23
        if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
24
            if (is_a($this, CachedModel::class)) {
25
                array_push($tags, str_slug(get_called_class()));
26
            }
27
28
            $cache = $cache->tags($tags);
29
        }
30
31
        return $cache;
32
    }
33
34
    public function disableCache()
35
    {
36
        session(['genealabs-laravel-model-caching-is-disabled' => true]);
37
        $this->isCachable = false;
38
39
        return $this;
40
    }
41
42
    public function flushCache(array $tags = [])
43
    {
44
        $this->cache($tags)->flush();
45
    }
46
47
    protected function makeCacheKey(
48
        array $columns = ['*'],
49
        $idColumn = null,
50
        string $keyDifferentiator = ''
51
    ) : string {
52
        $eagerLoad = $this->eagerLoad ?? [];
53
        $model = $this->model ?? $this;
54
        $query = $this->query ?? app(Builder::class);
1 ignored issue
show
Bug Best Practice introduced by
The property query does not exist on GeneaLabs\LaravelModelCaching\Traits\Cachable. Did you maybe forget to declare it?
Loading history...
55
56
        return (new CacheKey($eagerLoad, $model, $query))
1 ignored issue
show
Bug introduced by
It seems like $model can also be of type GeneaLabs\LaravelModelCaching\Traits\Cachable; however, parameter $model of GeneaLabs\LaravelModelCa...CacheKey::__construct() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        return (new CacheKey($eagerLoad, /** @scrutinizer ignore-type */ $model, $query))
Loading history...
57
            ->make($columns, $idColumn, $keyDifferentiator);
58
    }
59
60
    protected function makeCacheTags() : array
61
    {
62
        return (new CacheTags($this->eagerLoad, $this->model))
63
            ->make();
64
    }
65
66
    public static function bootCachable()
67
    {
68
        static::saved(function ($instance) {
69
            $instance->flushCache();
70
        });
71
    }
72
73
    public static function all($columns = ['*'])
74
    {
75
        $class = get_called_class();
76
        $instance = new $class;
77
        $tags = [str_slug(get_called_class())];
78
        $key = $instance->makeCacheKey();
79
80
        return $instance->cache($tags)
81
            ->rememberForever($key, function () use ($columns) {
82
                return parent::all($columns);
83
            });
84
    }
85
86
    public function newEloquentBuilder($query)
87
    {
88
        if (session('genealabs-laravel-model-caching-is-disabled')) {
89
            session()->forget('genealabs-laravel-model-caching-is-disabled');
90
91
            return new EloquentBuilder($query);
92
        }
93
94
        return new CachedBuilder($query);
95
    }
96
}
97