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 | 3 | 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) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Build a base query without sorting and limits from filter rules. |
||
| 144 | * |
||
| 145 | * @param array $rules |
||
| 146 | * @param boolean $count |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 2 | protected function buildQueryFromRules(array $rules, $count = false) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Asserts that a field is acceptable to filter on. |
||
| 184 | * |
||
| 185 | * @param string $name |
||
| 186 | * |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | 2 | protected function acceptableField($name) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | 1 | public function countByField($field, $value, ServerRequestInterface $request = null) |
|
| 209 | { |
||
| 210 | 1 | $query = sprintf( |
|
| 211 | 1 | "SELECT COUNT(*) as total FROM %s WHERE %s.%s IN (:%s)", |
|
| 212 | 1 | $this->getTable(), |
|
| 213 | 1 | $this->getTable(), |
|
| 214 | 1 | $field, |
|
| 215 | $field |
||
| 216 | 1 | ); |
|
| 217 | |||
| 218 | $params = [ |
||
| 219 | $field => $value |
||
| 220 | 1 | ]; |
|
| 221 | |||
| 222 | 1 | return (int) $this->dbal->fetchOne($query, $params)['total']; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 1 | public function getByField($field, $value, ServerRequestInterface $request = null) |
|
| 229 | { |
||
| 230 | 1 | $query = sprintf( |
|
| 231 | 1 | 'SELECT * FROM %s WHERE %s.%s IN (:%s)', |
|
| 232 | 1 | $this->getTable(), |
|
| 233 | 1 | $this->getTable(), |
|
| 234 | 1 | $field, |
|
| 235 | $field |
||
| 236 | 1 | ); |
|
| 237 | |||
| 238 | // @todo - allow extra filtering from request |
||
| 239 | |||
| 240 | $params = [ |
||
| 241 | $field => $value |
||
| 242 | 1 | ]; |
|
| 243 | |||
| 244 | 1 | $collection = $this->buildCollection($this->dbal->fetchAll($query, $params)) |
|
| 245 | 1 | ->setTotal($this->countByField($field, $value)); |
|
| 246 | |||
| 247 | 1 | $this->decorate($collection, StoreInterface::ON_READ); |
|
| 248 | |||
| 249 | 1 | return $collection; |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function attachRelationships( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Iterate a result set and attach the relationship to it's correct entity |
||
| 317 | * within a collection. |
||
| 318 | * |
||
| 319 | * @param \Percy\Entity\Collection $collection |
||
| 320 | * @param string $relationship |
||
| 321 | * @param array $data |
||
| 322 | * |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Return relationship bind conditional. |
||
| 359 | * |
||
| 360 | * @param \Percy\Entity\Collection $collection |
||
| 361 | * @param string $relationship |
||
| 362 | * @param string $key |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get possible relationships and the properties attached to them. |
||
| 383 | * |
||
| 384 | * @param string $relationship |
||
| 385 | * |
||
| 386 | * @throws \InvalidArgumentException when requested relationship is not defined |
||
| 387 | * @throws \RuntimeException when map structure is defined incorrectly |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function getRelationshipMap($relationship = null) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns table that repository is reading from. |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | abstract protected function getTable(); |
||
| 437 | } |
||
| 438 |