Passed
Pull Request — master (#138)
by Mike
03:16
created

ModelCaching::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
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
        // TODO: figure out how to add this listener
41
        // static::restored(function ($instance) {
42
        //     $instance->checkCooldownAndFlushAfterPersiting($instance);
43
        // });
44
45
        static::pivotAttached(function ($instance) {
46
            $instance->checkCooldownAndFlushAfterPersiting($instance);
47
        });
48
49
        static::pivotDetached(function ($instance) {
50
            $instance->checkCooldownAndFlushAfterPersiting($instance);
51
        });
52
53
        static::pivotUpdated(function ($instance) {
54
            $instance->checkCooldownAndFlushAfterPersiting($instance);
55
        });
56
    }
57
58
    public static function destroy($ids)
59
    {
60
        $class = get_called_class();
61
        $instance = new $class;
62
        $instance->flushCache();
63
64
        return parent::destroy($ids);
65
    }
66
67
    public function newEloquentBuilder($query)
68
    {
69
        if (! $this->isCachable()) {
70
            $this->isCachable = true;
71
72
            return new EloquentBuilder($query);
73
        }
74
75
        return new CachedBuilder($query);
76
    }
77
78
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
79
    {
80
        if ($this->isCachable()) {
81
            $query = $query->disableModelCaching();
82
        }
83
84
        return $query;
85
    }
86
87
    public function scopeWithCacheCooldownSeconds(
88
        EloquentBuilder $query,
89
        int $seconds
90
    ) : EloquentBuilder {
91
        $cachePrefix = $this->getCachePrefix();
92
        $modelClassName = get_class($this);
93
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
94
95
        $this->cache()
96
            ->rememberForever($cacheKey, function () use ($seconds) {
97
                return $seconds;
98
            });
99
100
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
101
        $this->cache()
102
            ->rememberForever($cacheKey, function () {
103
                return (new Carbon)->now();
104
            });
105
106
        return $query;
107
    }
108
}
109