Total Complexity | 40 |
Total Lines | 205 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CacheKey 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 CacheKey, and based on these observations, apply Extract Interface, too.
1 | <?php namespace GeneaLabs\LaravelModelCaching; |
||
7 | class CacheKey |
||
8 | { |
||
9 | protected $eagerLoad; |
||
10 | protected $model; |
||
11 | protected $query; |
||
12 | |||
13 | protected function getCachePrefix() : string |
||
14 | { |
||
15 | return "genealabs:laravel-model-caching:" |
||
16 | . (config("laravel-model-caching.cache-prefix") |
||
17 | ? config("laravel-model-caching.cache-prefix", "") . ":" |
||
18 | : ""); |
||
19 | } |
||
20 | |||
21 | public function __construct( |
||
22 | array $eagerLoad, |
||
23 | Model $model, |
||
24 | Builder $query |
||
25 | ) { |
||
26 | $this->eagerLoad = $eagerLoad; |
||
27 | $this->model = $model; |
||
28 | $this->query = $query; |
||
29 | } |
||
30 | |||
31 | public function make( |
||
32 | array $columns = ["*"], |
||
33 | $idColumn = null, |
||
34 | string $keyDifferentiator = "" |
||
35 | ) : string { |
||
36 | $key = $this->getCachePrefix(); |
||
37 | $key .= $this->getModelSlug(); |
||
38 | $key .= $this->getIdColumn($idColumn ?: ""); |
||
39 | $key .= $this->getQueryColumns($columns); |
||
40 | $key .= $this->getWhereClauses(); |
||
41 | $key .= $this->getWithModels(); |
||
42 | $key .= $this->getOrderByClauses(); |
||
43 | $key .= $this->getOffsetClause(); |
||
44 | $key .= $this->getLimitClause(); |
||
45 | $key .= $keyDifferentiator; |
||
46 | |||
47 | return $key; |
||
48 | } |
||
49 | |||
50 | protected function getIdColumn(string $idColumn) : string |
||
51 | { |
||
52 | return $idColumn ? "_{$idColumn}" : ""; |
||
53 | } |
||
54 | |||
55 | protected function getLimitClause() : string |
||
62 | } |
||
63 | |||
64 | protected function getModelSlug() : string |
||
67 | } |
||
68 | |||
69 | protected function getOffsetClause() : string |
||
70 | { |
||
71 | if (! $this->query->offset) { |
||
72 | return ""; |
||
73 | } |
||
74 | |||
75 | return "-offset_{$this->query->offset}"; |
||
76 | } |
||
77 | |||
78 | protected function getOrderByClauses() : string |
||
91 | } |
||
92 | |||
93 | protected function getQueryColumns(array $columns) : string |
||
94 | { |
||
95 | if ($columns === ["*"] || $columns === []) { |
||
96 | return ""; |
||
97 | } |
||
98 | |||
99 | return "_" . implode("_", $columns); |
||
100 | } |
||
101 | |||
102 | protected function getTypeClause($where) : string |
||
109 | } |
||
110 | |||
111 | protected function getValuesClause(array $where = null) : string |
||
112 | { |
||
113 | if (in_array($where["type"], ["NotNull"])) { |
||
114 | return ""; |
||
115 | } |
||
116 | |||
117 | $values = $this->getValuesFromWhere($where); |
||
1 ignored issue
–
show
|
|||
118 | $values = $this->getValuesFromBindings($values); |
||
119 | |||
120 | return "_" . $values; |
||
121 | } |
||
122 | |||
123 | protected function getValuesFromWhere(array $where) : string |
||
124 | { |
||
125 | return is_array(array_get($where, "values")) |
||
126 | ? implode("_", $where["values"]) |
||
127 | : ""; |
||
128 | } |
||
129 | |||
130 | protected function getValuesFromBindings(string $values) : string |
||
131 | { |
||
132 | if (! $values && $this->query->bindings["where"] ?? false) { |
||
133 | $values = implode("_", $this->query->bindings["where"]); |
||
134 | } |
||
135 | |||
136 | return $values; |
||
137 | } |
||
138 | |||
139 | protected function getWhereClauses(array $wheres = []) : string |
||
151 | } |
||
152 | |||
153 | protected function getNestedClauses(array $where) : string |
||
154 | { |
||
155 | if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) { |
||
156 | return ""; |
||
157 | } |
||
158 | |||
159 | return "_" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres); |
||
160 | } |
||
161 | |||
162 | protected function getColumnClauses(array $where) : string |
||
163 | { |
||
164 | if ($where["type"] !== "Column") { |
||
165 | return ""; |
||
166 | } |
||
167 | |||
168 | return "_{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}"; |
||
169 | } |
||
170 | |||
171 | protected function getRawClauses(array $where) : string |
||
172 | { |
||
173 | if ($where["type"] !== "raw") { |
||
174 | return ""; |
||
175 | } |
||
176 | |||
177 | return "_{$where["boolean"]}_" . str_slug($where["sql"]); |
||
178 | } |
||
179 | |||
180 | protected function getOtherClauses(array $where, string $carry = null) : string |
||
190 | } |
||
191 | |||
192 | protected function getWheres(array $wheres) : Collection |
||
201 | } |
||
202 | |||
203 | protected function getWithModels() : string |
||
212 | } |
||
213 | } |
||
214 |