Completed
Push — master ( db70b3...5ed9da )
by Mike
12:00 queued 06:05
created

CachedBuilder::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php namespace GeneaLabs\LaravelModelCaching;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
5
6
/**
7
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
8
 */
9
class CachedBuilder extends EloquentBuilder
10
{
11
    use Cachable;
12
13
    protected $isCachable = true;
14
15 View Code Duplication
    public function avg($column)
16
    {
17
        if (! $this->isCachable) {
18
            return parent::avg($column);
0 ignored issues
show
introduced by
The method avg() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

18
            return parent::/** @scrutinizer ignore-call */ avg($column);
Loading history...
19
        }
20
21
        return $this->cache($this->makeCacheTags())
22
            ->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
23
                return parent::avg($column);
24
            });
25
    }
26
27 View Code Duplication
    public function count($columns = ['*'])
28
    {
29
        if (! $this->isCachable) {
30
            return parent::count($columns);
1 ignored issue
show
introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

30
            return parent::/** @scrutinizer ignore-call */ count($columns);
Loading history...
31
        }
32
33
        return $this->cache($this->makeCacheTags())
34
            ->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
35
                return parent::count($columns);
36
            });
37
    }
38
39
    public function cursor()
40
    {
41
        if (! $this->isCachable) {
42
            return collect(parent::cursor());
43
        }
44
45
        return $this->cache($this->makeCacheTags())
46
            ->rememberForever($this->makeCacheKey() . "-cursor", function () {
47
                return collect(parent::cursor());
48
            });
49
    }
50
51
    public function delete()
52
    {
53
        $this->cache($this->makeCacheTags())
54
            ->flush();
55
56
        return parent::delete();
57
    }
58
59
    public function disableCache()
60
    {
61
        $this->isCachable = false;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @SuppressWarnings(PHPMD.ShortVariable)
68
     */
69
    public function find($id, $columns = ['*'])
70
    {
71
        if (! $this->isCachable) {
72
            return parent::find($id, $columns);
73
        }
74
75
        return $this->cache($this->makeCacheTags())
76
            ->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
77
                return parent::find($id, $columns);
78
            });
79
    }
80
81 View Code Duplication
    public function first($columns = ['*'])
82
    {
83
        if (! $this->isCachable) {
84
            return parent::first($columns);
85
        }
86
87
        return $this->cache($this->makeCacheTags())
88
            ->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
89
                return parent::first($columns);
90
            });
91
    }
92
93 View Code Duplication
    public function get($columns = ['*'])
94
    {
95
        if (! $this->isCachable) {
96
            return parent::get($columns);
97
        }
98
99
        return $this->cache($this->makeCacheTags())
100
            ->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
101
                return parent::get($columns);
102
            });
103
    }
104
105 View Code Duplication
    public function max($column)
106
    {
107
        if (! $this->isCachable) {
108
            return parent::max($column);
0 ignored issues
show
introduced by
The method max() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

108
            return parent::/** @scrutinizer ignore-call */ max($column);
Loading history...
109
        }
110
111
        return $this->cache($this->makeCacheTags())
112
            ->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
113
                return parent::max($column);
114
            });
115
    }
116
117 View Code Duplication
    public function min($column)
118
    {
119
        if (! $this->isCachable) {
120
            return parent::min($column);
0 ignored issues
show
introduced by
The method min() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

120
            return parent::/** @scrutinizer ignore-call */ min($column);
Loading history...
121
        }
122
123
        return $this->cache($this->makeCacheTags())
124
            ->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
125
                return parent::min($column);
126
            });
127
    }
128
129
    public function pluck($column, $key = null)
130
    {
131
        if (! $this->isCachable) {
132
            return parent::pluck($column, $key);
133
        }
134
135
        $cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
136
137
        if ($key) {
138
            $cacheKey .= "_{$key}";
139
        }
140
141
        return $this->cache($this->makeCacheTags())
142
            ->rememberForever($cacheKey, function () use ($column, $key) {
143
                return parent::pluck($column, $key);
144
            });
145
    }
146
147 View Code Duplication
    public function sum($column)
148
    {
149
        if (! $this->isCachable) {
150
            return parent::sum($column);
0 ignored issues
show
introduced by
The method sum() does not exist on Illuminate\Database\Eloquent\Builder. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

150
            return parent::/** @scrutinizer ignore-call */ sum($column);
Loading history...
151
        }
152
153
        return $this->cache($this->makeCacheTags())
154
            ->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
155
                return parent::sum($column);
156
            });
157
    }
158
159
    protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
160
    {
161
        return (new CacheKey($this->eagerLoad, $this->model, $this->query))
162
            ->make($columns, $idColumn);
163
    }
164
165
    protected function makeCacheTags() : array
166
    {
167
        return (new CacheTags($this->eagerLoad, $this->model))
168
            ->make();
169
    }
170
}
171