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 | 5 | 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 | 3 | 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) |
|
265 | { |
||
266 | 2 | $query = sprintf( |
|
267 | 2 | 'SELECT * FROM %s WHERE %s.%s IN (:%s)', |
|
268 | 2 | $this->getTable(), |
|
269 | 2 | $this->getTable(), |
|
270 | 2 | $field, |
|
271 | $field |
||
272 | 2 | ); |
|
273 | |||
274 | 2 | $rules = []; |
|
275 | |||
276 | 2 | if ($request instanceof ServerRequestInterface) { |
|
277 | 2 | $rules = $this->parseQueryString($request->getUri()->getQuery()); |
|
278 | 2 | } |
|
279 | |||
280 | 2 | if (is_array($value)) { |
|
281 | 2 | if (! array_key_exists('sort', $rules)) { |
|
282 | 1 | $query .= sprintf( |
|
283 | 1 | ' ORDER BY FIND_IN_SET(%s.%s, ' . $this->dbal->quote(implode(',', $value)) . ')', |
|
284 | 1 | $this->getTable(), |
|
285 | $field |
||
286 | 1 | ); |
|
287 | 1 | } else { |
|
288 | 1 | $entity = $this->getEntityType(); |
|
289 | 1 | $entity = new $entity; |
|
290 | 1 | $mapping = $entity->getMapping(); |
|
291 | 1 | $whitelist = array_keys($mapping); |
|
292 | 1 | $query .= $this->buildSortPart($rules['sort'], $this->getTable(), $whitelist); |
|
293 | } |
||
294 | 2 | } |
|
295 | |||
296 | // @todo - allow extra filtering from request |
||
297 | |||
298 | $params = [ |
||
299 | $field => $value |
||
300 | 2 | ]; |
|
301 | |||
302 | 2 | $collection = $this->buildCollection($this->dbal->fetchAll($query, $params)) |
|
303 | 2 | ->setTotal($this->countByField($field, $value)); |
|
304 | |||
305 | 2 | $this->decorate($collection, StoreInterface::ON_READ); |
|
306 | |||
307 | 2 | return $collection; |
|
308 | } |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | 1 | public function attachRelationships( |
|
388 | |||
389 | /** |
||
390 | * Build conditionals part of query to filter relationships. |
||
391 | * |
||
392 | * @param array $map |
||
393 | * @param array $filters |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | protected function buildRelationshipFilterQueryPart($map, $filters) |
||
413 | |||
414 | /** |
||
415 | * Iterate a result set and attach the relationship to it's correct entity |
||
416 | * within a collection. |
||
417 | * |
||
418 | * @param \Percy\Entity\Collection $collection |
||
419 | * @param string $relationship |
||
420 | * @param array $data |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
||
455 | |||
456 | /** |
||
457 | * Return relationship bind conditional. |
||
458 | * |
||
459 | * @param \Percy\Entity\Collection $collection |
||
460 | * @param string $relationship |
||
461 | * @param string $key |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
||
479 | |||
480 | /** |
||
481 | * Get possible relationships and the properties attached to them. |
||
482 | * |
||
483 | * @param string $relationship |
||
484 | * |
||
485 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
486 | * @throws \RuntimeException when map structure is defined incorrectly |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | 1 | public function getRelationshipMap($relationship = null) |
|
491 | { |
||
492 | 1 | if (is_null($relationship)) { |
|
493 | return $this->relationships; |
||
494 | } |
||
495 | |||
496 | 1 | if (! array_key_exists($relationship, $this->relationships)) { |
|
497 | throw new InvalidArgumentException( |
||
498 | sprintf('(%s) is not defined in the relationship map on (%s)', $relationship, get_class($this)) |
||
499 | ); |
||
500 | } |
||
501 | |||
502 | 1 | $map = $this->relationships[$relationship]; |
|
503 | |||
504 | foreach ([ |
||
505 | 1 | 'defined_in' => ['table', 'primary', 'entity'], |
|
506 | 1 | 'target' => ['table', 'primary', 'relationship'] |
|
507 | 1 | ] as $key => $value) { |
|
508 | 1 | if (! array_key_exists($key, $map) || ! is_array($map[$key])) { |
|
509 | throw new RuntimeException( |
||
510 | sprintf( |
||
511 | 'Relationship (%s) should contain the (%s) key and should be of type array on (%s)', |
||
512 | $relationship, $key, get_class($this) |
||
513 | ) |
||
514 | ); |
||
515 | } |
||
516 | |||
517 | 1 | if (! empty(array_diff($value, array_keys($map[$key])))) { |
|
518 | throw new RuntimeException( |
||
519 | sprintf( |
||
520 | '(%s) for relationship (%s) should contain keys (%s) on (%s)', |
||
521 | $key, $relationship, implode(', ', $value), get_class($this) |
||
522 | ) |
||
523 | ); |
||
524 | } |
||
525 | 1 | } |
|
526 | |||
527 | 1 | return $map; |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * Returns table that repository is reading from. |
||
532 | * |
||
533 | * @return string |
||
534 | */ |
||
535 | abstract protected function getTable(); |
||
536 | } |
||
537 |