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 | 4 | 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 | 2 | 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 | $keyword = (array_key_exists('filter', $rules)) ? ' AND' : ' WHERE'; |
||
186 | |||
187 | foreach ($rules['has'] as $has) { |
||
188 | $relationship = $this->getRelationshipMap($has); |
||
189 | |||
190 | $query .= sprintf( |
||
191 | '%s (SELECT COUNT(%s.%s) FROM %s WHERE %s.%s = %s.%s) > 0', |
||
192 | $keyword, |
||
193 | $relationship['defined_in']['table'], |
||
194 | $relationship['defined_in']['primary'], |
||
195 | $relationship['defined_in']['table'], |
||
196 | $relationship['defined_in']['table'], |
||
197 | $relationship['defined_in']['primary'], |
||
198 | $this->getTable(), |
||
199 | $relationship['defined_in']['entity'] |
||
200 | ); |
||
201 | |||
202 | $keyword = ' AND'; |
||
203 | } |
||
204 | } |
||
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 | 1 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 1 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
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 | public function getRelationshipMap($relationship = null) |
||
529 | |||
530 | /** |
||
531 | * Returns table that repository is reading from. |
||
532 | * |
||
533 | * @return string |
||
534 | */ |
||
535 | abstract protected function getTable(); |
||
536 | } |
||
537 |