| Total Complexity | 44 |
| Total Lines | 299 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CachedBuilder 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 CachedBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace GeneaLabs\LaravelModelCaching; |
||
| 11 | class CachedBuilder extends EloquentBuilder |
||
| 12 | { |
||
| 13 | use BuilderCaching; |
||
| 14 | use Caching; |
||
|
1 ignored issue
–
show
|
|||
| 15 | |||
| 16 | public function avg($column) |
||
| 25 | } |
||
| 26 | |||
| 27 | public function count($columns = "*") |
||
| 36 | } |
||
| 37 | |||
| 38 | public function decrement($column, $amount = 1, array $extra = []) |
||
| 39 | { |
||
| 40 | $this->cache($this->makeCacheTags()) |
||
| 41 | ->flush(); |
||
| 42 | |||
| 43 | return parent::decrement($column, $amount, $extra); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function delete() |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
| 56 | */ |
||
| 57 | public function find($id, $columns = ["*"]) |
||
| 58 | { |
||
| 59 | if (! $this->isCachable()) { |
||
| 60 | return parent::find($id, $columns); |
||
| 61 | } |
||
| 62 | |||
| 63 | $idKey = collect($id) |
||
| 64 | ->implode('_'); |
||
| 65 | $preStr = is_array($id) |
||
| 66 | ? 'find_list' |
||
| 67 | : 'find'; |
||
| 68 | $cacheKey = $this->makeCacheKey($columns, null, "-{$preStr}_{$idKey}"); |
||
| 69 | |||
| 70 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function first($columns = ["*"]) |
||
| 74 | { |
||
| 75 | if (! $this->isCachable()) { |
||
| 76 | return parent::first($columns); |
||
| 77 | } |
||
| 78 | |||
| 79 | $cacheKey = $this->makeCacheKey($columns, null, "-first"); |
||
| 80 | |||
| 81 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function forceDelete() |
||
| 85 | { |
||
| 86 | $this->cache($this->makeCacheTags()) |
||
| 87 | ->flush(); |
||
| 88 | |||
| 89 | return parent::forceDelete(); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function get($columns = ["*"]) |
||
| 93 | { |
||
| 94 | if (! $this->isCachable()) { |
||
| 95 | return parent::get($columns); |
||
| 96 | } |
||
| 97 | |||
| 98 | $cacheKey = $this->makeCacheKey($columns); |
||
| 99 | |||
| 100 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function increment($column, $amount = 1, array $extra = []) |
||
| 104 | { |
||
| 105 | $this->cache($this->makeCacheTags()) |
||
| 106 | ->flush(); |
||
| 107 | |||
| 108 | return parent::increment($column, $amount, $extra); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function inRandomOrder($seed = '') |
||
| 112 | { |
||
| 113 | $this->isCachable = false; |
||
| 114 | |||
| 115 | return parent::inRandomOrder($seed); |
||
|
1 ignored issue
–
show
|
|||
| 116 | } |
||
| 117 | |||
| 118 | public function insert(array $values) |
||
| 119 | { |
||
| 120 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
| 121 | |||
| 122 | return parent::insert($values); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function max($column) |
||
| 126 | { |
||
| 127 | if (! $this->isCachable()) { |
||
| 128 | return parent::max($column); |
||
| 129 | } |
||
| 130 | |||
| 131 | $cacheKey = $this->makeCacheKey(["*"], null, "-max_{$column}"); |
||
| 132 | |||
| 133 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function min($column) |
||
| 145 | } |
||
| 146 | |||
| 147 | public function paginate( |
||
| 148 | $perPage = null, |
||
| 149 | $columns = ["*"], |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the relation instance for the given relation name. |
||
| 171 | * This is overloaded so we can disable model cache on |
||
| 172 | * relations if parent has disabled model caching. |
||
| 173 | * |
||
| 174 | * @param string $name |
||
| 175 | * @return \Illuminate\Database\Eloquent\Relations\Relation |
||
| 176 | */ |
||
| 177 | public function getRelation($name) |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string |
||
| 189 | { |
||
| 190 | $result = ""; |
||
| 191 | |||
| 192 | foreach ($items as $key => $value) { |
||
| 193 | if (is_array($value)) { |
||
| 194 | $result .= $key . $glue . $this->recursiveImplodeWithKey($value, $glue); |
||
| 195 | |||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | $result .= $glue . $key . $glue . $value; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $result; |
||
| 203 | } |
||
| 204 | |||
| 205 | public function pluck($column, $key = null) |
||
| 215 | } |
||
| 216 | |||
| 217 | public function sum($column) |
||
| 218 | { |
||
| 219 | if (! $this->isCachable()) { |
||
| 220 | return parent::sum($column); |
||
| 221 | } |
||
| 222 | |||
| 223 | $cacheKey = $this->makeCacheKey(["*"], null, "-sum_{$column}"); |
||
| 224 | |||
| 225 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function update(array $values) |
||
| 229 | { |
||
| 230 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
| 231 | |||
| 232 | return parent::update($values); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function value($column) |
||
| 244 | } |
||
| 245 | |||
| 246 | public function cachedValue(array $arguments, string $cacheKey) |
||
| 247 | { |
||
| 248 | $method = debug_backtrace()[1]['function']; |
||
| 249 | $cacheTags = $this->makeCacheTags(); |
||
| 250 | $hashedCacheKey = sha1($cacheKey); |
||
| 251 | $result = $this->retrieveCachedValue( |
||
| 252 | $arguments, |
||
| 253 | $cacheKey, |
||
| 254 | $cacheTags, |
||
| 255 | $hashedCacheKey, |
||
| 256 | $method |
||
| 257 | ); |
||
| 258 | |||
| 259 | return $this->preventHashCollision( |
||
| 260 | $result, |
||
| 261 | $arguments, |
||
| 262 | $cacheKey, |
||
| 263 | $cacheTags, |
||
| 264 | $hashedCacheKey, |
||
| 265 | $method |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | protected function preventHashCollision( |
||
| 270 | array $result, |
||
| 271 | array $arguments, |
||
| 272 | string $cacheKey, |
||
| 273 | array $cacheTags, |
||
| 274 | string $hashedCacheKey, |
||
| 275 | string $method |
||
| 276 | ) { |
||
| 277 | if ($result["key"] === $cacheKey) { |
||
| 278 | return $result["value"]; |
||
| 279 | } |
||
| 280 | |||
| 281 | $this->cache() |
||
| 282 | ->tags($cacheTags) |
||
| 283 | ->forget($hashedCacheKey); |
||
| 284 | |||
| 285 | return $this->retrieveCachedValue( |
||
| 286 | $arguments, |
||
| 287 | $cacheKey, |
||
| 288 | $cacheTags, |
||
| 289 | $hashedCacheKey, |
||
| 290 | $method |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | protected function retrieveCachedValue( |
||
| 310 | ]; |
||
| 311 | } |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 |