Total Complexity | 44 |
Total Lines | 287 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Buildable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Buildable, and based on these observations, apply Extract Interface, too.
1 | <?php namespace GeneaLabs\LaravelModelCaching\Traits; |
||
6 | trait Buildable |
||
7 | { |
||
8 | public function avg($column) |
||
9 | { |
||
10 | if (! $this->isCachable()) { |
||
11 | return parent::avg($column); |
||
12 | } |
||
13 | |||
14 | $cacheKey = $this->makeCacheKey(["*"], null, "-avg_{$column}"); |
||
15 | |||
16 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
17 | } |
||
18 | |||
19 | public function count($columns = "*") |
||
20 | { |
||
21 | if (! $this->isCachable()) { |
||
22 | return parent::count($columns); |
||
23 | } |
||
24 | |||
25 | $cacheKey = $this->makeCacheKey([$columns], null, "-count"); |
||
26 | |||
27 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
28 | } |
||
29 | |||
30 | public function decrement($column, $amount = 1, array $extra = []) |
||
31 | { |
||
32 | $this->cache($this->makeCacheTags()) |
||
33 | ->flush(); |
||
34 | |||
35 | return parent::decrement($column, $amount, $extra); |
||
36 | } |
||
37 | |||
38 | public function delete() |
||
39 | { |
||
40 | $this->cache($this->makeCacheTags()) |
||
41 | ->flush(); |
||
42 | |||
43 | return parent::delete(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
48 | */ |
||
49 | public function find($id, $columns = ["*"]) |
||
50 | { |
||
51 | if (! $this->isCachable()) { |
||
52 | return parent::find($id, $columns); |
||
53 | } |
||
54 | |||
55 | $idKey = collect($id) |
||
56 | ->implode('_'); |
||
57 | $preStr = is_array($id) |
||
58 | ? 'find_list' |
||
59 | : 'find'; |
||
60 | $cacheKey = $this->makeCacheKey($columns, null, "-{$preStr}_{$idKey}"); |
||
61 | |||
62 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
63 | } |
||
64 | |||
65 | public function first($columns = ["*"]) |
||
66 | { |
||
67 | if (! $this->isCachable()) { |
||
68 | return parent::first($columns); |
||
69 | } |
||
70 | |||
71 | if (! is_array($columns)) { |
||
72 | $columns = [$columns]; |
||
73 | } |
||
74 | |||
75 | $cacheKey = $this->makeCacheKey($columns, null, "-first"); |
||
76 | |||
77 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
78 | } |
||
79 | |||
80 | public function forceDelete() |
||
81 | { |
||
82 | $this->cache($this->makeCacheTags()) |
||
83 | ->flush(); |
||
84 | |||
85 | return parent::forceDelete(); |
||
86 | } |
||
87 | |||
88 | public function get($columns = ["*"]) |
||
89 | { |
||
90 | if (! $this->isCachable()) { |
||
91 | return parent::get($columns); |
||
92 | } |
||
93 | |||
94 | $cacheKey = $this->makeCacheKey($columns); |
||
95 | |||
96 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
97 | } |
||
98 | |||
99 | public function increment($column, $amount = 1, array $extra = []) |
||
100 | { |
||
101 | $this->cache($this->makeCacheTags()) |
||
102 | ->flush(); |
||
103 | |||
104 | return parent::increment($column, $amount, $extra); |
||
105 | } |
||
106 | |||
107 | public function inRandomOrder($seed = '') |
||
112 | } |
||
113 | |||
114 | public function insert(array $values) |
||
115 | { |
||
116 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
117 | |||
119 | } |
||
120 | |||
121 | public function max($column) |
||
122 | { |
||
123 | if (! $this->isCachable()) { |
||
124 | return parent::max($column); |
||
125 | } |
||
126 | |||
127 | $cacheKey = $this->makeCacheKey(["*"], null, "-max_{$column}"); |
||
128 | |||
129 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
130 | } |
||
131 | |||
132 | public function min($column) |
||
141 | } |
||
142 | |||
143 | public function paginate( |
||
144 | $perPage = null, |
||
145 | $columns = ["*"], |
||
163 | } |
||
164 | |||
165 | protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string |
||
166 | { |
||
167 | $result = ""; |
||
168 | |||
169 | foreach ($items as $key => $value) { |
||
170 | if (is_array($value)) { |
||
171 | $result .= $key . $glue . $this->recursiveImplodeWithKey($value, $glue); |
||
172 | |||
173 | continue; |
||
174 | } |
||
175 | |||
176 | $result .= $glue . $key . $glue . $value; |
||
177 | } |
||
178 | |||
179 | return $result; |
||
180 | } |
||
181 | |||
182 | public function pluck($column, $key = null) |
||
192 | } |
||
193 | |||
194 | public function sum($column) |
||
195 | { |
||
196 | if (! $this->isCachable()) { |
||
197 | return parent::sum($column); |
||
198 | } |
||
199 | |||
200 | $cacheKey = $this->makeCacheKey(["*"], null, "-sum_{$column}"); |
||
201 | |||
202 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
203 | } |
||
204 | |||
205 | public function update(array $values) |
||
206 | { |
||
207 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
208 | |||
209 | return parent::update($values); |
||
210 | } |
||
211 | |||
212 | public function value($column) |
||
213 | { |
||
214 | if (! $this->isCachable()) { |
||
215 | return parent::value($column); |
||
216 | } |
||
217 | |||
218 | $cacheKey = $this->makeCacheKey(["*"], null, "-value_{$column}"); |
||
219 | |||
220 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
221 | } |
||
222 | |||
223 | public function cachedValue(array $arguments, string $cacheKey) |
||
224 | { |
||
225 | $method = debug_backtrace()[1]['function']; |
||
226 | $cacheTags = $this->makeCacheTags(); |
||
227 | $hashedCacheKey = sha1($cacheKey); |
||
228 | $result = $this->retrieveCachedValue( |
||
229 | $arguments, |
||
230 | $cacheKey, |
||
231 | $cacheTags, |
||
232 | $hashedCacheKey, |
||
233 | $method |
||
234 | ); |
||
235 | |||
236 | return $this->preventHashCollision( |
||
237 | $result, |
||
238 | $arguments, |
||
239 | $cacheKey, |
||
240 | $cacheTags, |
||
241 | $hashedCacheKey, |
||
242 | $method |
||
243 | ); |
||
244 | } |
||
245 | |||
246 | protected function preventHashCollision( |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | protected function retrieveCachedValue( |
||
272 | array $arguments, |
||
293 | ]; |
||
294 | } |
||
298 |