Passed
Push — master ( ad40e4...eeb78e )
by Mike
02:44
created

ModelCaching::getUseCacheCooldownAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
    protected $cacheCooldownSeconds = 0;
10
11
    public static function all($columns = ['*'])
12
    {
13
        if (config('laravel-model-caching.disabled')) {
14
            return parent::all($columns);
15
        }
16
17
        $class = get_called_class();
18
        $instance = new $class;
19
        $tags = $instance->makeCacheTags();
20
        $key = $instance->makeCacheKey();
21
22
        return $instance->cache($tags)
23
            ->rememberForever($key, function () use ($columns) {
24
                return parent::all($columns);
25
            });
26
    }
27
28
    public static function bootCachable()
29
    {
30
        static::created(function ($instance) {
31
            $instance->checkCooldownAndFlushAfterPersisting($instance);
32
        });
33
34
        static::deleted(function ($instance) {
35
            $instance->checkCooldownAndFlushAfterPersisting($instance);
36
        });
37
38
        static::saved(function ($instance) {
39
            $instance->checkCooldownAndFlushAfterPersisting($instance);
40
        });
41
42
        // TODO: figure out how to add this listener
43
        // static::restored(function ($instance) {
44
        //     $instance->checkCooldownAndFlushAfterPersisting($instance);
45
        // });
46
47
        static::pivotAttached(function ($instance) {
48
            $instance->checkCooldownAndFlushAfterPersisting($instance);
49
        });
50
51
        static::pivotDetached(function ($instance) {
52
            $instance->checkCooldownAndFlushAfterPersisting($instance);
53
        });
54
55
        static::pivotUpdated(function ($instance) {
56
            $instance->checkCooldownAndFlushAfterPersisting($instance);
57
        });
58
    }
59
60
    public static function destroy($ids)
61
    {
62
        $class = get_called_class();
63
        $instance = new $class;
64
        $instance->flushCache();
65
66
        return parent::destroy($ids);
67
    }
68
69
    public function newEloquentBuilder($query)
70
    {
71
        if (! $this->isCachable()) {
72
            $this->isCachable = true;
73
74
            return new EloquentBuilder($query);
75
        }
76
77
        return new CachedBuilder($query);
78
    }
79
80
    public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
81
    {
82
        if ($this->isCachable()) {
83
            $query = $query->disableModelCaching();
84
        }
85
86
        return $query;
87
    }
88
89
    public function scopeWithCacheCooldownSeconds(
90
        EloquentBuilder $query,
91
        int $seconds = null
92
    ) : EloquentBuilder {
93
        if (! $seconds) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $seconds of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
94
            $seconds = $this->cacheCooldownSeconds;
95
        }
96
97
        $cachePrefix = $this->getCachePrefix();
98
        $modelClassName = get_class($this);
99
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
100
101
        $this->cache()
102
            ->rememberForever($cacheKey, function () use ($seconds) {
103
                return $seconds;
104
            });
105
106
        $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at";
107
        $this->cache()
108
            ->rememberForever($cacheKey, function () {
109
                return (new Carbon)->now();
110
            });
111
112
        return $query;
113
    }
114
115
    public function getcacheCooldownSecondsAttribute() : bool
116
    {
117
        return $this->cacheCooldownSeconds;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cacheCooldownSeconds returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
118
    }
119
}
120