| Total Complexity | 47 |
| Total Lines | 304 |
| 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; |
||
| 9 | trait Buildable |
||
| 10 | { |
||
| 11 | public function avg($column) |
||
| 12 | { |
||
| 13 | if (! $this->isCachable()) { |
||
|
1 ignored issue
–
show
|
|||
| 14 | return parent::avg($column); |
||
| 15 | } |
||
| 16 | |||
| 17 | $cacheKey = $this->makeCacheKey(["*"], null, "-avg_{$column}"); |
||
|
1 ignored issue
–
show
|
|||
| 18 | |||
| 19 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function count($columns = "*") |
||
| 23 | { |
||
| 24 | if (! $this->isCachable()) { |
||
| 25 | return parent::count($columns); |
||
| 26 | } |
||
| 27 | |||
| 28 | $cacheKey = $this->makeCacheKey([$columns], null, "-count"); |
||
| 29 | |||
| 30 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function decrement($column, $amount = 1, array $extra = []) |
||
| 34 | { |
||
| 35 | $this->cache($this->makeCacheTags()) |
||
|
2 ignored issues
–
show
|
|||
| 36 | ->flush(); |
||
| 37 | |||
| 38 | return parent::decrement($column, $amount, $extra); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function delete() |
||
| 42 | { |
||
| 43 | $this->cache($this->makeCacheTags()) |
||
| 44 | ->flush(); |
||
| 45 | |||
| 46 | return parent::delete(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
| 51 | */ |
||
| 52 | public function find($id, $columns = ["*"]) |
||
| 53 | { |
||
| 54 | if (! $this->isCachable()) { |
||
| 55 | return parent::find($id, $columns); |
||
| 56 | } |
||
| 57 | |||
| 58 | $idKey = collect($id) |
||
| 59 | ->implode('_'); |
||
| 60 | $preStr = is_array($id) |
||
| 61 | ? 'find_list' |
||
| 62 | : 'find'; |
||
| 63 | $cacheKey = $this->makeCacheKey($columns, null, "-{$preStr}_{$idKey}"); |
||
| 64 | |||
| 65 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function first($columns = ["*"]) |
||
| 69 | { |
||
| 70 | if (! $this->isCachable()) { |
||
| 71 | return parent::first($columns); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (! is_array($columns)) { |
||
| 75 | $columns = [$columns]; |
||
| 76 | } |
||
| 77 | |||
| 78 | $cacheKey = $this->makeCacheKey($columns, null, "-first"); |
||
| 79 | |||
| 80 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function forceDelete() |
||
| 84 | { |
||
| 85 | $this->cache($this->makeCacheTags()) |
||
| 86 | ->flush(); |
||
| 87 | |||
| 88 | return parent::forceDelete(); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function get($columns = ["*"]) |
||
| 92 | { |
||
| 93 | if (! $this->isCachable()) { |
||
| 94 | return parent::get($columns); |
||
| 95 | } |
||
| 96 | |||
| 97 | $cacheKey = $this->makeCacheKey($columns); |
||
| 98 | |||
| 99 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function increment($column, $amount = 1, array $extra = []) |
||
| 103 | { |
||
| 104 | $this->cache($this->makeCacheTags()) |
||
| 105 | ->flush(); |
||
| 106 | |||
| 107 | return parent::increment($column, $amount, $extra); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function inRandomOrder($seed = '') |
||
| 115 | } |
||
| 116 | |||
| 117 | public function insert(array $values) |
||
| 118 | { |
||
| 119 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
|
1 ignored issue
–
show
|
|||
| 120 | |||
| 121 | return parent::insert($values); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function max($column) |
||
| 125 | { |
||
| 126 | if (! $this->isCachable()) { |
||
| 127 | return parent::max($column); |
||
| 128 | } |
||
| 129 | |||
| 130 | $cacheKey = $this->makeCacheKey(["*"], null, "-max_{$column}"); |
||
| 131 | |||
| 132 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function min($column) |
||
| 136 | { |
||
| 137 | if (! $this->isCachable()) { |
||
| 138 | return parent::min($column); |
||
| 139 | } |
||
| 140 | |||
| 141 | $cacheKey = $this->makeCacheKey(["*"], null, "-min_{$column}"); |
||
| 142 | |||
| 143 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function paginate( |
||
| 147 | $perPage = null, |
||
| 148 | $columns = ["*"], |
||
| 149 | $pageName = "page", |
||
| 150 | $page = null |
||
| 151 | ) { |
||
| 152 | if (! $this->isCachable()) { |
||
| 153 | return parent::paginate($perPage, $columns, $pageName, $page); |
||
| 154 | } |
||
| 155 | |||
| 156 | $page = app('request')->input($pageName) |
||
| 157 | ?: $page |
||
| 158 | ?: 1; |
||
| 159 | |||
| 160 | if (is_array($page)) { |
||
| 161 | $page = $this->recursiveImplodeWithKey($page); |
||
| 162 | } |
||
| 163 | $cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}"); |
||
| 164 | |||
| 165 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getRelation($name) |
||
| 169 | { |
||
| 170 | $relation = parent::getRelation($name); |
||
| 171 | |||
| 172 | if (! $this->isCachable() |
||
| 173 | && is_a($relation->getQuery(), self::class) |
||
| 174 | ) { |
||
| 175 | $relation->getQuery()->disableModelCaching(); |
||
|
1 ignored issue
–
show
|
|||
| 176 | } |
||
| 177 | |||
| 178 | return $relation; |
||
| 179 | } |
||
| 180 | |||
| 181 | protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string |
||
| 182 | { |
||
| 183 | $result = ""; |
||
| 184 | |||
| 185 | foreach ($items as $key => $value) { |
||
| 186 | if (is_array($value)) { |
||
| 187 | $result .= $key . $glue . $this->recursiveImplodeWithKey($value, $glue); |
||
| 188 | |||
| 189 | continue; |
||
| 190 | } |
||
| 191 | |||
| 192 | $result .= $glue . $key . $glue . $value; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $result; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function pluck($column, $key = null) |
||
| 208 | } |
||
| 209 | |||
| 210 | public function sum($column) |
||
| 211 | { |
||
| 212 | if (! $this->isCachable()) { |
||
| 213 | return parent::sum($column); |
||
| 214 | } |
||
| 215 | |||
| 216 | $cacheKey = $this->makeCacheKey(["*"], null, "-sum_{$column}"); |
||
| 217 | |||
| 218 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function update(array $values) |
||
| 222 | { |
||
| 223 | $this->checkCooldownAndFlushAfterPersisting($this->model); |
||
| 224 | |||
| 225 | return parent::update($values); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function value($column) |
||
| 229 | { |
||
| 230 | if (! $this->isCachable()) { |
||
| 231 | return parent::value($column); |
||
| 232 | } |
||
| 233 | |||
| 234 | $cacheKey = $this->makeCacheKey(["*"], null, "-value_{$column}"); |
||
| 235 | |||
| 236 | return $this->cachedValue(func_get_args(), $cacheKey); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function cachedValue(array $arguments, string $cacheKey) |
||
| 240 | { |
||
| 241 | $method = debug_backtrace()[1]['function']; |
||
| 242 | $cacheTags = $this->makeCacheTags(); |
||
| 243 | $hashedCacheKey = sha1($cacheKey); |
||
| 244 | $result = $this->retrieveCachedValue( |
||
| 245 | $arguments, |
||
| 246 | $cacheKey, |
||
| 247 | $cacheTags, |
||
| 248 | $hashedCacheKey, |
||
| 249 | $method |
||
| 250 | ); |
||
| 251 | |||
| 252 | return $this->preventHashCollision( |
||
| 253 | $result, |
||
| 254 | $arguments, |
||
| 255 | $cacheKey, |
||
| 256 | $cacheTags, |
||
| 257 | $hashedCacheKey, |
||
| 258 | $method |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 262 | protected function preventHashCollision( |
||
| 284 | ); |
||
| 285 | } |
||
| 286 | |||
| 287 | protected function retrieveCachedValue( |
||
| 288 | array $arguments, |
||
| 289 | string $cacheKey, |
||
| 290 | array $cacheTags, |
||
| 291 | string $hashedCacheKey, |
||
| 292 | string $method |
||
| 293 | ) { |
||
| 294 | $model = new BaseModel; |
||
| 313 | ]; |
||
| 314 | } |
||
| 315 | ); |
||
| 316 | } |
||
| 317 | } |
||
| 318 |