Test Failed
Push — master ( 9098a2...b88650 )
by Mike
08:28
created

Cachable   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 104
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A newEloquentBuilder() 0 9 2
A addTagsWhenCalledFromCachedBuilder() 0 10 2
A cache() 0 14 3
A makeCacheTags() 0 6 1
A flushCache() 0 7 2
A makeCacheKey() 0 11 1
A bootCachable() 0 4 1
A all() 0 10 1
A disableCache() 0 5 1
A isCachable() 0 4 2
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
            $tags = $this->addTagsWhenCalledFromCachedBuilder($tags);
25
            $cache = $cache->tags($tags);
26
        }
27
28
        return $cache;
29
    }
30
31
    protected function addTagsWhenCalledFromCachedBuilder(array $tags) : array
32
    {
33
        $usesCachableTrait = collect(class_uses($this))
34
            ->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
35
36
        if (! $usesCachableTrait) {
37
            array_push($tags, str_slug(get_called_class()));
38
        }
39
40
        return $tags;
41
    }
42
43
    public function disableCache()
44
    {
45
        $this->isCachable = false;
46
47
        return $this;
48
    }
49
50
    public function flushCache(array $tags = [])
51
    {
52
        if (count($tags) === 0) {
53
            $tags = $this->makeCacheTags();
54
        }
55
56
        $this->cache($tags)->flush();
57
    }
58
59
    protected function makeCacheKey(
60
        array $columns = ['*'],
61
        $idColumn = null,
62
        string $keyDifferentiator = ''
63
    ) : string {
64
        $eagerLoad = $this->eagerLoad ?? [];
65
        $model = $this->model ?? $this;
66
        $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...
67
68
        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

68
        return (new CacheKey($eagerLoad, /** @scrutinizer ignore-type */ $model, $query))
Loading history...
69
            ->make($columns, $idColumn, $keyDifferentiator);
70
    }
71
72
    protected function makeCacheTags() : array
73
    {
74
        $tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this))
1 ignored issue
show
Bug introduced by
It seems like $this->model ?? $this can also be of type GeneaLabs\LaravelModelCaching\Traits\Cachable; however, parameter $model of GeneaLabs\LaravelModelCa...acheTags::__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

74
        $tags = (new CacheTags($this->eagerLoad ?? [], /** @scrutinizer ignore-type */ $this->model ?? $this))
Loading history...
75
            ->make();
76
77
        return $tags;
78
    }
79
80
    public static function bootCachable()
81
    {
82
        static::saved(function ($instance) {
83
            $instance->flushCache();
84
        });
85
    }
86
87
    public static function all($columns = ['*'])
88
    {
89
        $class = get_called_class();
90
        $instance = new $class;
91
        $tags = [str_slug(get_called_class())];
92
        $key = $instance->makeCacheKey();
93
94
        return $instance->cache($tags)
95
            ->rememberForever($key, function () use ($columns) {
96
                return parent::all($columns);
97
            });
98
    }
99
100
    public function newEloquentBuilder($query)
101
    {
102
        if (! $this->isCachable()) {
103
            $this->isCachable = true;
104
105
            return new EloquentBuilder($query);
106
        }
107
108
        return new CachedBuilder($query);
109
    }
110
111
    public function isCachable() : bool
112
    {
113
        return $this->isCachable
114
            && ! config('laravel-model-caching.disabled');
115
    }
116
}
117