Complex classes like AbstractSqlRepository 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractSqlRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class AbstractSqlRepository implements RepositoryInterface |
||
17 | { |
||
18 | use CollectionBuilderTrait; |
||
19 | use DecoratorTrait; |
||
20 | use QueryStringParserTrait; |
||
21 | |||
22 | /** |
||
23 | * @var \Aura\Sql\ExtendedPdoInterface |
||
24 | */ |
||
25 | protected $dbal; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var mixed |
||
30 | */ |
||
31 | protected $relationships = []; |
||
32 | |||
33 | /** |
||
34 | * Construct. |
||
35 | * |
||
36 | * @param \Aura\Sql\ExtendedPdoInterface $dbal |
||
37 | */ |
||
38 | 6 | public function __construct(ExtendedPdoInterface $dbal) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1 | public function countFromRequest(ServerRequestInterface $request) |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 2 | public function getFromRequest(ServerRequestInterface $request) |
|
91 | |||
92 | /** |
||
93 | * Build the sort part of the query. |
||
94 | * |
||
95 | * @param array|string $sorts |
||
96 | * @param string $table |
||
97 | * @param array $whitelist |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | 4 | protected function buildSortPart($sorts, $table, array $whitelist) |
|
145 | |||
146 | /** |
||
147 | * Build a base query without sorting and limits from filter rules. |
||
148 | * |
||
149 | * @param array $rules |
||
150 | * @param boolean $count |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 2 | protected function buildQueryFromRules(array $rules, $count = false) |
|
155 | { |
||
156 | 2 | $start = ($count === false) ? 'SELECT * FROM ' : 'SELECT *, COUNT(*) as total FROM '; |
|
157 | 2 | $query = $start . $this->getTable(); |
|
158 | |||
159 | 2 | $params = []; |
|
160 | |||
161 | 2 | if (array_key_exists('filter', $rules)) { |
|
162 | 2 | foreach ($rules['filter'] as $key => $where) { |
|
163 | 2 | $this->acceptableField($where['field']); |
|
164 | |||
165 | 1 | $isNull = ($where['value'] === 'null') ? true : false; |
|
166 | |||
167 | 1 | $keyword = ($key === 0) ? ' WHERE' : ' AND'; |
|
168 | 1 | $delimiter = strtoupper($where['delimiter']); |
|
169 | 1 | $binding = (in_array($delimiter, ['IN', 'NOT IN'])) ? '(' . $this->dbal->quote(explode(',', $where['value'])) . ')' : ':' . $where['binding']; |
|
170 | |||
171 | 1 | if ($isNull === true) { |
|
172 | 1 | $delimiter = ($delimiter === '=') ? 'IS' : 'IS NOT'; |
|
173 | 1 | $binding = 'null'; |
|
174 | 1 | } |
|
175 | |||
176 | 1 | $query .= sprintf('%s %s %s %s', $keyword, $where['field'], $delimiter, $binding); |
|
177 | |||
178 | 1 | if (! in_array($delimiter, ['IN', 'NOT IN'])) { |
|
179 | 1 | $params[$where['binding']] = $where['value']; |
|
180 | 1 | } |
|
181 | 1 | } |
|
182 | 1 | } |
|
183 | |||
184 | 2 | if (array_key_exists('has', $rules)) { |
|
185 | 1 | $keyword = (array_key_exists('filter', $rules)) ? ' AND' : ' WHERE'; |
|
186 | |||
187 | 1 | foreach ($rules['has'] as $has) { |
|
188 | 1 | $relationship = $this->getRelationshipMap($has); |
|
189 | |||
190 | 1 | $query .= sprintf( |
|
191 | 1 | '%s (SELECT COUNT(%s.%s) FROM %s WHERE %s.%s = %s.%s) > 0', |
|
192 | 1 | $keyword, |
|
193 | 1 | $relationship['defined_in']['table'], |
|
194 | 1 | $relationship['defined_in']['primary'], |
|
195 | 1 | $relationship['defined_in']['table'], |
|
196 | 1 | $relationship['defined_in']['table'], |
|
197 | 1 | $relationship['defined_in']['primary'], |
|
198 | 1 | $this->getTable(), |
|
199 | 1 | $relationship['defined_in']['entity'] |
|
200 | 1 | ); |
|
201 | |||
202 | 1 | $keyword = ' AND'; |
|
203 | 1 | } |
|
204 | 1 | } |
|
205 | |||
206 | 2 | if (array_key_exists('search', $rules) && $this->acceptableField($rules['search']['fields'])) { |
|
207 | 1 | $keyword = (array_key_exists('filter', $rules)) ? ' AND' : ' WHERE'; |
|
208 | 1 | $query .= sprintf('%s MATCH (%s) AGAINST (:match_bind IN BOOLEAN MODE)', $keyword, $rules['search']['fields']); |
|
209 | 1 | $query .= sprintf(' HAVING MATCH (%s) AGAINST (:match_bind) > :score_bind', $rules['search']['fields']); |
|
210 | |||
211 | 1 | $params['match_bind'] = $rules['search']['term']; |
|
212 | 1 | $params['score_bind'] = (array_key_exists('minscore', $rules)) ? $rules['minscore'] : 0; |
|
213 | 1 | } |
|
214 | |||
215 | 2 | return [$query, $params]; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Asserts that a field is acceptable to filter on. |
||
220 | * |
||
221 | * @param string $name |
||
222 | * |
||
223 | * @return boolean |
||
224 | */ |
||
225 | 2 | protected function acceptableField($name) |
|
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | 2 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 2 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | 2 | public function attachRelationships( |
|
401 | |||
402 | /** |
||
403 | * Build conditionals part of query to filter relationships. |
||
404 | * |
||
405 | * @param array $map |
||
406 | * @param array $filters |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 1 | protected function buildRelationshipFilterQueryPart($map, $filters) |
|
426 | |||
427 | /** |
||
428 | * Iterate a result set and attach the relationship to it's correct entity |
||
429 | * within a collection. |
||
430 | * |
||
431 | * @param \Percy\Entity\Collection $collection |
||
432 | * @param string $relationship |
||
433 | * @param array $data |
||
434 | * |
||
435 | * @return void |
||
436 | */ |
||
437 | 1 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
|
468 | |||
469 | /** |
||
470 | * Return relationship bind conditional. |
||
471 | * |
||
472 | * @param \Percy\Entity\Collection $collection |
||
473 | * @param string $relationship |
||
474 | * @param string $key |
||
475 | * |
||
476 | * @return string |
||
477 | */ |
||
478 | 1 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
|
492 | |||
493 | /** |
||
494 | * Get possible relationships and the properties attached to them. |
||
495 | * |
||
496 | * @param string $relationship |
||
497 | * |
||
498 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
499 | * @throws \RuntimeException when map structure is defined incorrectly |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | 2 | public function getRelationshipMap($relationship = null) |
|
542 | |||
543 | /** |
||
544 | * Returns table that repository is reading from. |
||
545 | * |
||
546 | * @return string |
||
547 | */ |
||
548 | abstract protected function getTable(); |
||
549 | } |
||
550 |