Total Complexity | 46 |
Total Lines | 311 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CacheBelongsToMany 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 CacheBelongsToMany, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class CacheBelongsToMany extends BelongsToMany |
||
10 | { |
||
11 | use BuilderCaching, |
||
12 | Caching, |
||
13 | FiresPivotEventsTrait; |
||
14 | |||
15 | protected function makeCacheKey( |
||
16 | array $columns = ['*'], |
||
17 | $idColumn = null, |
||
18 | string $keyDifferentiator = '' |
||
19 | ) : string { |
||
20 | $eagerLoad = $this->eagerLoad ?? []; |
||
21 | $model = $this->model ?? $this; |
||
22 | $query = $this->query->getQuery() ?? app('db')->query(); |
||
23 | |||
24 | return (new CacheKey($eagerLoad, $model, $query)) |
||
25 | ->make($columns, $idColumn, $keyDifferentiator); |
||
26 | } |
||
27 | |||
28 | public function avg($column) |
||
29 | { |
||
30 | if (! $this->isCachable()) { |
||
31 | return parent::avg($column); |
||
32 | } |
||
33 | |||
34 | $cacheKey = $this->makeCacheKey(["*"], null, "-avg_{$column}"); |
||
35 | |||
36 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
37 | } |
||
38 | |||
39 | public function count($columns = "*") |
||
40 | { |
||
41 | if (! $this->isCachable()) { |
||
42 | return parent::count($columns); |
||
43 | } |
||
44 | |||
45 | $cacheKey = $this->makeCacheKey([$columns], null, "-count"); |
||
46 | |||
47 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
48 | } |
||
49 | |||
50 | public function decrement($column, $amount = 1, array $extra = []) |
||
51 | { |
||
52 | $this->cache($this->makeCacheTags()) |
||
53 | ->flush(); |
||
54 | |||
55 | return parent::decrement($column, $amount, $extra); |
||
56 | } |
||
57 | |||
58 | public function delete() |
||
59 | { |
||
60 | $this->cache($this->makeCacheTags()) |
||
61 | ->flush(); |
||
62 | |||
63 | return parent::delete(); |
||
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 | $idKey = collect($id) |
||
76 | ->implode('_'); |
||
77 | $preStr = is_array($id) |
||
78 | ? 'find_list' |
||
79 | : 'find'; |
||
80 | $cacheKey = $this->makeCacheKey($columns, null, "-{$preStr}_{$idKey}"); |
||
81 | |||
82 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
83 | } |
||
84 | |||
85 | public function first($columns = ["*"]) |
||
86 | { |
||
87 | if (! $this->isCachable()) { |
||
88 | return parent::first($columns); |
||
89 | } |
||
90 | |||
91 | if (! is_array($columns)) { |
||
92 | $columns = [$columns]; |
||
93 | } |
||
94 | |||
95 | $cacheKey = $this->makeCacheKey($columns, null, "-first"); |
||
96 | |||
97 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
98 | } |
||
99 | |||
100 | public function forceDelete() |
||
101 | { |
||
102 | $this->cache($this->makeCacheTags()) |
||
103 | ->flush(); |
||
104 | |||
105 | return parent::forceDelete(); |
||
106 | } |
||
107 | |||
108 | public function get($columns = ["*"]) |
||
117 | } |
||
118 | |||
119 | public function increment($column, $amount = 1, array $extra = []) |
||
120 | { |
||
121 | $this->cache($this->makeCacheTags()) |
||
122 | ->flush(); |
||
123 | |||
124 | return parent::increment($column, $amount, $extra); |
||
125 | } |
||
126 | |||
127 | public function inRandomOrder($seed = '') |
||
128 | { |
||
129 | $this->isCachable = false; |
||
130 | |||
131 | return parent::inRandomOrder($seed); |
||
132 | } |
||
133 | |||
134 | public function insert(array $values) |
||
135 | { |
||
136 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
137 | |||
138 | return parent::insert($values); |
||
139 | } |
||
140 | |||
141 | public function max($column) |
||
142 | { |
||
143 | if (! $this->isCachable()) { |
||
144 | return parent::max($column); |
||
145 | } |
||
146 | |||
147 | $cacheKey = $this->makeCacheKey(["*"], null, "-max_{$column}"); |
||
148 | |||
149 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
150 | } |
||
151 | |||
152 | public function min($column) |
||
153 | { |
||
154 | if (! $this->isCachable()) { |
||
155 | return parent::min($column); |
||
156 | } |
||
157 | |||
158 | $cacheKey = $this->makeCacheKey(["*"], null, "-min_{$column}"); |
||
159 | |||
160 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
161 | } |
||
162 | |||
163 | public function paginate( |
||
164 | $perPage = null, |
||
165 | $columns = ["*"], |
||
166 | $pageName = "page", |
||
167 | $page = null |
||
168 | ) { |
||
169 | if (! $this->isCachable()) { |
||
170 | return parent::paginate($perPage, $columns, $pageName, $page); |
||
171 | } |
||
172 | |||
173 | $page = app('request')->input($pageName) |
||
174 | ?: $page |
||
175 | ?: 1; |
||
176 | |||
177 | if (is_array($page)) { |
||
178 | $page = $this->recursiveImplodeWithKey($page); |
||
179 | } |
||
180 | $cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}"); |
||
181 | |||
182 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
183 | } |
||
184 | |||
185 | public function getRelation($name) |
||
186 | { |
||
187 | $relation = parent::getRelation($name); |
||
188 | |||
189 | if (! $this->isCachable() |
||
190 | && is_a($relation->getQuery(), self::class) |
||
191 | ) { |
||
192 | $relation->getQuery()->disableModelCaching(); |
||
193 | } |
||
194 | |||
195 | return $relation; |
||
196 | } |
||
197 | |||
198 | protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string |
||
199 | { |
||
200 | $result = ""; |
||
201 | |||
202 | foreach ($items as $key => $value) { |
||
203 | if (is_array($value)) { |
||
204 | $result .= $key . $glue . $this->recursiveImplodeWithKey($value, $glue); |
||
205 | |||
206 | continue; |
||
207 | } |
||
208 | |||
209 | $result .= $glue . $key . $glue . $value; |
||
210 | } |
||
211 | |||
212 | return $result; |
||
213 | } |
||
214 | |||
215 | public function pluck($column, $key = null) |
||
216 | { |
||
217 | if (! $this->isCachable()) { |
||
218 | return parent::pluck($column, $key); |
||
219 | } |
||
220 | |||
221 | $keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : ""); |
||
222 | $cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator); |
||
223 | |||
224 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
225 | } |
||
226 | |||
227 | public function sum($column) |
||
228 | { |
||
229 | if (! $this->isCachable()) { |
||
230 | return parent::sum($column); |
||
231 | } |
||
232 | |||
233 | $cacheKey = $this->makeCacheKey(["*"], null, "-sum_{$column}"); |
||
234 | |||
235 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
236 | } |
||
237 | |||
238 | public function update(array $values) |
||
239 | { |
||
240 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
241 | |||
242 | return parent::update($values); |
||
243 | } |
||
244 | |||
245 | public function value($column) |
||
246 | { |
||
247 | if (! $this->isCachable()) { |
||
248 | return parent::value($column); |
||
249 | } |
||
250 | |||
251 | $cacheKey = $this->makeCacheKey(["*"], null, "-value_{$column}"); |
||
252 | |||
253 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
254 | } |
||
255 | |||
256 | public function cachedValue(array $arguments, string $cacheKey) |
||
276 | ); |
||
277 | } |
||
278 | |||
279 | protected function preventHashCollision( |
||
280 | array $result, |
||
281 | array $arguments, |
||
282 | string $cacheKey, |
||
283 | array $cacheTags, |
||
284 | string $hashedCacheKey, |
||
285 | string $method |
||
286 | ) { |
||
287 | if ($result["key"] === $cacheKey) { |
||
288 | return $result["value"]; |
||
289 | } |
||
290 | |||
291 | $this->cache() |
||
292 | ->tags($cacheTags) |
||
293 | ->forget($hashedCacheKey); |
||
294 | |||
295 | return $this->retrieveCachedValue( |
||
296 | $arguments, |
||
297 | $cacheKey, |
||
298 | $cacheTags, |
||
299 | $hashedCacheKey, |
||
300 | $method |
||
301 | ); |
||
302 | } |
||
303 | |||
304 | protected function retrieveCachedValue( |
||
320 | ]; |
||
321 | } |
||
322 | ); |
||
323 | } |
||
324 | } |
||
325 |