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 | 2 | public function __construct(ExtendedPdoInterface $dbal) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 1 | public function countFromRequest(ServerRequestInterface $request) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 1 | public function getFromRequest(ServerRequestInterface $request) |
|
| 58 | { |
||
| 59 | 1 | $rules = $this->parseQueryString($request->getUri()->getQuery()); |
|
| 60 | |||
| 61 | 1 | list($query, $params) = $this->buildQueryFromRules($rules); |
|
| 62 | |||
| 63 | 1 | if (array_key_exists('sort', $rules) && ! array_key_exists('search', $rules)) { |
|
| 64 | 1 | $query .= $this->buildSortPart($rules['sort'], $this->getTable()); |
|
| 65 | 1 | } |
|
| 66 | |||
| 67 | 1 | if (array_key_exists('search', $rules) && $this->acceptableField($rules['search']['fields'])) { |
|
| 68 | 1 | $query .= sprintf(' ORDER BY MATCH (%s) AGAINST (:match_bind) > :score_bind', $rules['search']['fields']); |
|
| 69 | 1 | } |
|
| 70 | |||
| 71 | 1 | if (array_key_exists('limit', $rules)) { |
|
| 72 | 1 | $query .= ' LIMIT '; |
|
| 73 | 1 | $query .= (array_key_exists('offset', $rules)) ? sprintf('%d,', $rules['offset']) : ''; |
|
| 74 | 1 | $query .= sprintf('%d', $rules['limit']); |
|
| 75 | 1 | } |
|
| 76 | |||
| 77 | 1 | $query = trim(preg_replace('!\s+!', ' ', $query)); |
|
| 78 | |||
| 79 | 1 | $collection = $this->buildCollection($this->dbal->fetchAll($query, $params)) |
|
| 80 | 1 | ->setTotal($this->countFromRequest($request)); |
|
| 81 | |||
| 82 | 1 | $this->decorate($collection, StoreInterface::ON_READ); |
|
| 83 | |||
| 84 | 1 | return $collection; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Build the sort part of the query. |
||
| 89 | * |
||
| 90 | * @param array|string $sorts |
||
| 91 | * @param string $table |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 1 | protected function buildSortPart($sorts, $table) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Build a base query without sorting and limits from filter rules. |
||
| 120 | * |
||
| 121 | * @param array $rules |
||
| 122 | * @param boolean $count |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | 1 | protected function buildQueryFromRules(array $rules, $count = false) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Asserts that a field is acceptable to filter on. |
||
| 160 | * |
||
| 161 | * @param string $name |
||
| 162 | * |
||
| 163 | * @return boolean |
||
| 164 | */ |
||
| 165 | 1 | protected function acceptableField($name) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | 1 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | 1 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function attachRelationships( |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Iterate a result set and attach the relationship to it's correct entity |
||
| 280 | * within a collection. |
||
| 281 | * |
||
| 282 | * @param \Percy\Entity\Collection $collection |
||
| 283 | * @param string $relationship |
||
| 284 | * @param array $data |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return relationship bind conditional. |
||
| 322 | * |
||
| 323 | * @param \Percy\Entity\Collection $collection |
||
| 324 | * @param string $relationship |
||
| 325 | * @param string $key |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get possible relationships and the properties attached to them. |
||
| 346 | * |
||
| 347 | * @param string $relationship |
||
| 348 | * |
||
| 349 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
| 350 | * @throws \RuntimeException when map structure is defined incorrectly |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function getRelationshipMap($relationship = null) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns table that repository is reading from. |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | abstract protected function getTable(); |
||
| 400 | } |
||
| 401 |