Passed
Pull Request — master (#247)
by Mike
02:51
created

ModelCaching::scopeWithCacheCooldownSeconds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2
3
use Carbon\Carbon;
4
use GeneaLabs\LaravelModelCaching\CachedBelongsToMany;
5
use GeneaLabs\LaravelModelCaching\CachedBuilder;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
use Illuminate\Database\Eloquent\Model;
8
9
trait ModelCaching
10
{
11
    protected $cacheCooldownSeconds = 0;
12
13
    public static function all($columns = ['*'])
14
    {
15
        if (config('laravel-model-caching.disabled')) {
16
            return parent::all($columns);
17
        }
18
19
        $class = get_called_class();
20
        $instance = new $class;
21
        $tags = $instance->makeCacheTags();
22
        $key = $instance->makeCacheKey();
23
24
        return $instance->cache($tags)
25
            ->rememberForever($key, function () use ($columns) {
26
                return parent::all($columns);
27
            });
28
    }
29
30
    public static function bootCachable()
31
    {
32
        static::created(function ($instance) {
33
            $instance->checkCooldownAndFlushAfterPersisting($instance);
34
        });
35
36
        static::deleted(function ($instance) {
37
            $instance->checkCooldownAndFlushAfterPersisting($instance);
38
        });
39
40
        static::saved(function ($instance) {
41
            $instance->checkCooldownAndFlushAfterPersisting($instance);
42
        });
43
44
        // TODO: figure out how to add this listener
45
        // static::restored(function ($instance) {
46
        //     $instance->checkCooldownAndFlushAfterPersisting($instance);
47
        // });
48
49
        static::pivotAttached(function ($instance) {
50
            $instance->checkCooldownAndFlushAfterPersisting($instance);
51
        });
52
53
        static::pivotDetached(function ($instance) {
54
            $instance->checkCooldownAndFlushAfterPersisting($instance);
55
        });
56
57
        static::pivotUpdated(function ($instance) {
58
            $instance->checkCooldownAndFlushAfterPersisting($instance);
59
        });
60
    }
61
62
    public static function destroy($ids)
63
    {
64
        $class = get_called_class();
65
        $instance = new $class;
66
        $instance->flushCache();
67
68
        return parent::destroy($ids);
69
    }
70
71
    public function newEloquentBuilder($query)
72
    {
73
        if (! $this->isCachable()) {
74
            $this->isCachable = false;
75
76
            return new EloquentBuilder($query);
77
        }
78
79
        return new CachedBuilder($query);
80
    }
81
82
    protected function newBelongsToMany(
83
        EloquentBuilder $query,
84
        Model $parent,
85
        $table,
86
        $foreignPivotKey,
87
        $relatedPivotKey,
88
        $parentKey,
89
        $relatedKey,
90
        $relationName = null
91
    ) {
92
        if (get_class($query) === EloquentBuilder::class) {
93
            $query = new CachedBuilder($query);
0 ignored issues
show
Bug introduced by
$query of type Illuminate\Database\Eloquent\Builder is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $query of GeneaLabs\LaravelModelCa...dBuilder::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            $query = new CachedBuilder(/** @scrutinizer ignore-type */ $query);
Loading history...
94
        }
95
96
        return new CachedBelongsToMany(
97
            $query,
98
            $parent,
99
            $table,
100
            $foreignPivotKey,
101
            $relatedPivotKey,
102
            $parentKey,
103
            $relatedKey,
104
            $relationName
105
        );
106
    }
107
108
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
109
    {
110
        if ($this->isCachable()) {
111
            $query = $query->disableModelCaching();
112
        }
113
114
        return $query;
115
    }
116
117
    public function scopeWithCacheCooldownSeconds(
118
        EloquentBuilder $query,
119
        int $seconds = null
120
    ) : EloquentBuilder {
121
        if (! $seconds) {
122
            $seconds = $this->cacheCooldownSeconds;
123
        }
124
125
        $cachePrefix = $this->getCachePrefix();
126
        $modelClassName = get_class($this);
127
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
128
129
        $this->cache()
130
            ->rememberForever($cacheKey, function () use ($seconds) {
131
                return $seconds;
132
            });
133
134
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
135
        $this->cache()
136
            ->rememberForever($cacheKey, function () {
137
                return (new Carbon)->now();
138
            });
139
140
        return $query;
141
    }
142
143
    public function getcacheCooldownSecondsAttribute() : int
144
    {
145
        return $this->cacheCooldownSeconds;
146
    }
147
}
148