Passed
Pull Request — master (#138)
by Mike
03:13 queued 15s
created

ModelCaching::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use Carbon\Carbon;
4
use GeneaLabs\LaravelModelCaching\CachedBuilder;
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
7
trait ModelCaching
8
{
9
    public static function all($columns = ['*'])
10
    {
11
        if (config('laravel-model-caching.disabled')) {
12
            return parent::all($columns);
13
        }
14
15
        $class = get_called_class();
16
        $instance = new $class;
17
        $tags = $instance->makeCacheTags();
18
        $key = $instance->makeCacheKey();
19
20
        return $instance->cache($tags)
21
            ->rememberForever($key, function () use ($columns) {
22
                return parent::all($columns);
23
            });
24
    }
25
26
    public static function bootCachable()
27
    {
28
        static::created(function ($instance) {
29
            $instance->checkCooldownAndFlushAfterPersiting($instance);
30
        });
31
32
        static::deleted(function ($instance) {
33
            $instance->checkCooldownAndFlushAfterPersiting($instance);
34
        });
35
36
        static::saved(function ($instance) {
37
            $instance->checkCooldownAndFlushAfterPersiting($instance);
38
        });
39
40
        static::restored(function ($instance) {
41
            $instance->checkCooldownAndFlushAfterPersiting($instance);
42
        });
43
44
        static::pivotAttached(function ($instance) {
45
            $instance->checkCooldownAndFlushAfterPersiting($instance);
46
        });
47
48
        static::pivotDetached(function ($instance) {
49
            $instance->checkCooldownAndFlushAfterPersiting($instance);
50
        });
51
52
        static::pivotUpdated(function ($instance) {
53
            $instance->checkCooldownAndFlushAfterPersiting($instance);
54
        });
55
    }
56
57
    public static function destroy($ids)
58
    {
59
        $class = get_called_class();
60
        $instance = new $class;
61
        $instance->flushCache();
62
63
        return parent::destroy($ids);
64
    }
65
66
    public function newEloquentBuilder($query)
67
    {
68
        if (! $this->isCachable()) {
69
            $this->isCachable = true;
70
71
            return new EloquentBuilder($query);
72
        }
73
74
        return new CachedBuilder($query);
75
    }
76
77
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
78
    {
79
        if ($this->isCachable()) {
80
            $query = $query->disableModelCaching();
81
        }
82
83
        return $query;
84
    }
85
86
    public function scopeWithCacheCooldownSeconds(
87
        EloquentBuilder $query,
88
        int $seconds
89
    ) : EloquentBuilder {
90
        $cachePrefix = $this->getCachePrefix();
91
        $modelClassName = get_class($this);
92
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
93
94
        $this->cache()
95
            ->rememberForever($cacheKey, function () use ($seconds) {
96
                return $seconds;
97
            });
98
99
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
100
        $this->cache()
101
            ->rememberForever($cacheKey, function () {
102
                return (new Carbon)->now();
103
            });
104
105
        return $query;
106
    }
107
}
108