| Total Complexity | 26 |
| Total Lines | 122 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php namespace GeneaLabs\LaravelModelCaching\Traits; |
||
| 5 | trait CacheKeyable |
||
| 6 | { |
||
| 7 | protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string |
||
| 8 | { |
||
| 9 | $key = $this->getModelSlug(); |
||
| 10 | $key .= $this->getIdColumn($idColumn ?: ''); |
||
| 11 | $key .= $this->getQueryColumns($columns); |
||
| 12 | $key .= $this->getWhereClauses(); |
||
| 13 | $key .= $this->getWithModels(); |
||
| 14 | $key .= $this->getOrderByClauses(); |
||
| 15 | $key .= $this->getOffsetClause(); |
||
| 16 | $key .= $this->getLimitClause(); |
||
| 17 | |||
| 18 | return $key; |
||
| 19 | } |
||
| 20 | |||
| 21 | protected function getIdColumn(string $idColumn) : string |
||
| 22 | { |
||
| 23 | return $idColumn ? "_{$idColumn}" : ''; |
||
| 24 | } |
||
| 25 | |||
| 26 | protected function getLimitClause() : string |
||
| 27 | { |
||
| 28 | if (! $this->query->limit) { |
||
|
1 ignored issue
–
show
|
|||
| 29 | return ''; |
||
| 30 | } |
||
| 31 | |||
| 32 | return "-limit_{$this->query->limit}"; |
||
| 33 | } |
||
| 34 | |||
| 35 | protected function getModelSlug() : string |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function getOffsetClause() : string |
||
| 41 | { |
||
| 42 | if (! $this->query->offset) { |
||
|
1 ignored issue
–
show
|
|||
| 43 | return ''; |
||
| 44 | } |
||
| 45 | |||
| 46 | return "-offset_{$this->query->offset}"; |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function getOrderByClauses() : string |
||
| 57 | } |
||
| 58 | |||
| 59 | protected function getQueryColumns(array $columns) : string |
||
| 60 | { |
||
| 61 | if ($columns === ['*'] || $columns === []) { |
||
| 62 | return ''; |
||
| 63 | } |
||
| 64 | |||
| 65 | return '_' . implode('_', $columns); |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function getTypeClause($where) : string |
||
| 69 | { |
||
| 70 | return in_array($where['type'], ['In', 'Null', 'NotNull']) |
||
| 71 | ? strtolower($where['type']) |
||
| 72 | : ''; |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function getValuesClause(array $where = null) : string |
||
| 76 | { |
||
| 77 | return is_array(array_get($where, 'values')) |
||
| 78 | ? '_' . implode('_', $where['values']) |
||
| 79 | : ''; |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function getWhereClauses(array $wheres = []) : string |
||
| 105 | } |
||
| 106 | |||
| 107 | protected function getWheres(array $wheres) : Collection |
||
| 108 | { |
||
| 109 | $wheres = collect($wheres); |
||
| 110 | |||
| 111 | if ($wheres->isEmpty()) { |
||
| 112 | $wheres = collect($this->query->wheres); |
||
|
1 ignored issue
–
show
|
|||
| 113 | } |
||
| 114 | |||
| 115 | return $wheres; |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function getWithModels() : string |
||
| 127 | } |
||
| 128 | } |
||
| 129 |