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)) { |
|
| 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 .= $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) |
|
| 96 | { |
||
| 97 | 1 | if (is_string($sorts) && $sorts === 'RAND()') { |
|
| 98 | 1 | return ' ORDER BY RAND()'; |
|
| 99 | } |
||
| 100 | |||
| 101 | 1 | if (! is_array($sorts)) { |
|
| 102 | return ''; |
||
| 103 | } |
||
| 104 | |||
| 105 | 1 | $fields = []; |
|
| 106 | |||
| 107 | 1 | foreach ($sorts as $sort) { |
|
| 108 | 1 | if (substr($sort['field'], 0, strlen($table)) !== $table) { |
|
| 109 | 1 | continue; |
|
| 110 | } |
||
| 111 | |||
| 112 | 1 | $fields[] = sprintf('%s %s', $sort['field'], strtoupper($sort['direction'])); |
|
| 113 | 1 | } |
|
| 114 | |||
| 115 | 1 | return (empty($fields)) ? '' : sprintf(' ORDER BY %s', implode(', ', $fields)); |
|
| 116 | } |
||
| 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) |
|
| 127 | { |
||
| 128 | 1 | $start = ($count === false) ? 'SELECT * FROM ' : 'SELECT *, COUNT(*) as total FROM '; |
|
| 129 | |||
| 130 | 1 | $query = $start . $this->getTable(); |
|
| 131 | |||
| 132 | 1 | $params = []; |
|
| 133 | |||
| 134 | 1 | if (array_key_exists('filter', $rules)) { |
|
| 135 | 1 | foreach ($rules['filter'] as $key => $where) { |
|
| 136 | 1 | $keyword = ($key === 0) ? ' WHERE' : ' AND'; |
|
| 137 | 1 | $delimiter = strtoupper($where['delimiter']); |
|
| 138 | 1 | $binding = (in_array($delimiter, ['IN', 'NOT IN'])) ? sprintf('(:%s)', $where['binding']) : ':' . $where['binding']; |
|
| 139 | 1 | $query .= sprintf('%s %s %s %s', $keyword, $where['field'], $delimiter, $binding); |
|
| 140 | |||
| 141 | 1 | $params[$where['binding']] = $where['value']; |
|
| 142 | 1 | } |
|
| 143 | 1 | } |
|
| 144 | |||
| 145 | 1 | if (array_key_exists('search', $rules)) { |
|
| 146 | 1 | $keyword = (array_key_exists('filter', $rules)) ? ' AND' : ' WHERE'; |
|
| 147 | 1 | $query .= sprintf('%s MATCH (%s) AGAINST (:match_bind IN BOOLEAN MODE)', $keyword, $rules['search']['fields']); |
|
| 148 | 1 | $query .= sprintf(' HAVING MATCH (%s) AGAINST (:match_bind) > :score_bind', $rules['search']['fields']); |
|
| 149 | |||
| 150 | 1 | $params['match_bind'] = $rules['search']['term']; |
|
| 151 | 1 | $params['score_bind'] = (array_key_exists('minscore', $rules)) ? $rules['minscore'] : 0; |
|
| 152 | 1 | } |
|
| 153 | |||
| 154 | 1 | return [$query, $params]; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 1 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 1 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function attachRelationships( |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Iterate a result set and attach the relationship to it's correct entity |
||
| 256 | * within a collection. |
||
| 257 | * |
||
| 258 | * @param \Percy\Entity\Collection $collection |
||
| 259 | * @param string $relationship |
||
| 260 | * @param array $data |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Return relationship bind conditional. |
||
| 298 | * |
||
| 299 | * @param \Percy\Entity\Collection $collection |
||
| 300 | * @param string $relationship |
||
| 301 | * @param string $key |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get possible relationships and the properties attached to them. |
||
| 322 | * |
||
| 323 | * @param string $relationship |
||
| 324 | * |
||
| 325 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
| 326 | * @throws \RuntimeException when map structure is defined incorrectly |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getRelationshipMap($relationship = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns table that repository is reading from. |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | abstract protected function getTable(); |
||
| 376 | } |
||
| 377 |