Completed
Push — master ( b0dccf...a37019 )
by Mike
10s
created

Cachable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cache() 0 17 4
A makeCacheTags() 0 4 1
A flushCache() 0 3 1
A makeCacheKey() 0 4 1
A disableCache() 0 6 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
8
trait Cachable
9
{
10
    protected $isCachable = true;
11
12
    protected function cache(array $tags = [])
13
    {
14
        $cache = cache();
15
16
        if (config('laravel-model-caching.store')) {
17
            $cache = $cache->store(config('laravel-model-caching.store'));
18
        }
19
20
        if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
21
            if (is_a($this, CachedModel::class)) {
22
                array_push($tags, str_slug(get_called_class()));
23
            }
24
25
            $cache = $cache->tags($tags);
26
        }
27
28
        return $cache;
29
    }
30
31
    public function disableCache()
32
    {
33
        session(['genealabs-laravel-model-caching-is-disabled' => true]);
34
        $this->isCachable = false;
35
36
        return $this;
37
    }
38
39
    public function flushCache(array $tags = [])
40
    {
41
        $this->cache($tags)->flush();
42
    }
43
44
    protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
45
    {
46
        return (new CacheKey($this->eagerLoad, $this->model, $this->query))
3 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on GeneaLabs\LaravelModelCaching\Traits\Cachable. Did you maybe forget to declare it?
Loading history...
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...
Bug Best Practice introduced by
The property eagerLoad does not exist on GeneaLabs\LaravelModelCaching\Traits\Cachable. Did you maybe forget to declare it?
Loading history...
47
            ->make($columns, $idColumn);
48
    }
49
50
    protected function makeCacheTags() : array
51
    {
52
        return (new CacheTags($this->eagerLoad, $this->model))
2 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on GeneaLabs\LaravelModelCaching\Traits\Cachable. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property eagerLoad does not exist on GeneaLabs\LaravelModelCaching\Traits\Cachable. Did you maybe forget to declare it?
Loading history...
53
            ->make();
54
    }
55
}
56