Passed
Pull Request — master (#43)
by Mike
04:07
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
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