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 | 6 | 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 | 4 | 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 | 2 | public function attachRelationships( |
|
| 314 | Collection $collection, |
||
| 315 | $include = null, |
||
| 316 | ServerRequestInterface $request = null |
||
| 317 | ) { |
||
| 318 | 2 | if (count($collection) === 0) { |
|
| 319 | 1 | return; |
|
| 320 | } |
||
| 321 | |||
| 322 | 1 | $bind = []; |
|
| 323 | |||
| 324 | 1 | $rels = $collection->getIterator()->current()->getRelationshipMap(); |
|
| 325 | |||
| 326 | 1 | $rules = ($request instanceof ServerRequestInterface) |
|
| 327 | 1 | ? $this->parseQueryString($request->getUri()->getQuery()) |
|
| 328 | 1 | : []; |
|
| 329 | |||
| 330 | 1 | foreach ($this->getRelationshipMap() as $key => $map) { |
|
| 331 | if ( |
||
| 332 | 1 | ! array_key_exists('include', $rules) || |
|
| 333 | 1 | (array_key_exists('include', $rules) && ! array_key_exists($key, $rules['include'])) |
|
| 334 | 1 | ) { |
|
| 335 | continue; |
||
| 336 | } |
||
| 337 | |||
| 338 | 1 | $binds = $this->getRelationshipBinds($collection, $key, $map['defined_in']['entity']); |
|
| 339 | |||
| 340 | 1 | if (empty($binds)) { |
|
| 341 | continue; |
||
| 342 | } |
||
| 343 | |||
| 344 | 1 | $query = sprintf( |
|
| 345 | 1 | 'SELECT * FROM %s LEFT JOIN %s ON %s.%s = %s.%s WHERE %s.%s IN (:relationships)', |
|
| 346 | 1 | $map['defined_in']['table'], |
|
| 347 | 1 | $map['target']['table'], |
|
| 348 | 1 | $map['target']['table'], |
|
| 349 | 1 | $map['target']['primary'], |
|
| 350 | 1 | $map['defined_in']['table'], |
|
| 351 | 1 | $map['target']['relationship'], |
|
| 352 | 1 | $map['defined_in']['table'], |
|
| 353 | 1 | $map['defined_in']['primary'] |
|
| 354 | 1 | ); |
|
| 355 | |||
| 356 | 1 | $options = (array_key_exists('include', $rules)) ? $rules['include'][$key] : []; |
|
| 357 | |||
| 358 | 1 | if (! empty($options['filter'])) { |
|
| 359 | 1 | $query .= $this->buildRelationshipFilterQueryPart($map, $options['filter']); |
|
| 360 | |||
| 361 | 1 | foreach ($options['filter'] as $filter) { |
|
| 362 | 1 | $bind[$filter['binding']] = $filter['value']; |
|
| 363 | 1 | } |
|
| 364 | 1 | } |
|
| 365 | |||
| 366 | 1 | if (array_key_exists('sort', $rules)) { |
|
| 367 | 1 | $whitelist = []; |
|
| 368 | |||
| 369 | 1 | if (array_key_exists($key, $rels)) { |
|
| 370 | 1 | $entity = $rels[$key]; |
|
| 371 | 1 | $whitelist = array_keys((new $entity)->getMapping()); |
|
| 372 | 1 | } |
|
| 373 | |||
| 374 | 1 | $query .= $this->buildSortPart($rules['sort'], $map['target']['table'], $whitelist); |
|
| 375 | 1 | } |
|
| 376 | |||
| 377 | 1 | if (array_key_exists('limit', $rules) && ! is_null($rules['limit'])) { |
|
| 378 | 1 | $query .= ' LIMIT ' . (int) $rules['limit']; |
|
| 379 | 1 | } |
|
| 380 | |||
| 381 | 1 | $bind['relationships'] = $binds; |
|
| 382 | |||
| 383 | 1 | $result = $this->dbal->fetchAll($query, $bind); |
|
| 384 | |||
| 385 | 1 | $this->attachRelationshipsToCollection($collection, $key, $result); |
|
| 386 | 1 | } |
|
| 387 | 1 | } |
|
| 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 | 1 | protected function buildRelationshipFilterQueryPart($map, $filters) |
|
| 398 | { |
||
| 399 | 1 | $query = ''; |
|
| 400 | |||
| 401 | 1 | foreach ($filters as $filter) { |
|
| 402 | 1 | $query .= sprintf( |
|
| 403 | 1 | ' AND %s.%s %s :%s', |
|
| 404 | 1 | $map['target']['table'], |
|
| 405 | 1 | $filter['field'], |
|
| 406 | 1 | $filter['delimiter'], |
|
| 407 | 1 | $filter['binding'] |
|
| 408 | 1 | ); |
|
| 409 | 1 | } |
|
| 410 | |||
| 411 | 1 | return $query; |
|
| 412 | } |
||
| 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 | 1 | protected function attachRelationshipsToCollection(Collection $collection, $relationship, array $data) |
|
| 425 | { |
||
| 426 | 1 | $map = $this->getRelationshipMap($relationship); |
|
| 427 | 1 | $relationships = array_column($data, $map['defined_in']['primary']); |
|
| 428 | |||
| 429 | 1 | $remove = [$map['defined_in']['primary'], $map['target']['relationship']]; |
|
| 430 | |||
| 431 | 1 | foreach ($data as &$resource) { |
|
| 432 | $resource = array_filter($resource, function ($key) use ($remove) { |
||
| 433 | return (! in_array($key, $remove)); |
||
| 434 | }, ARRAY_FILTER_USE_KEY); |
||
| 435 | 1 | } |
|
| 436 | |||
| 437 | 1 | foreach ($collection->getIterator() as $entity) { |
|
| 438 | 1 | $entityRels = $entity->getRelationshipMap(); |
|
| 439 | |||
| 440 | 1 | if (! array_key_exists($relationship, $entityRels)) { |
|
| 441 | continue; |
||
| 442 | } |
||
| 443 | |||
| 444 | 1 | $keys = array_keys(preg_grep("/{$entity[$map['defined_in']['entity']]}/", $relationships)); |
|
| 445 | 1 | $rels = array_filter($data, function ($key) use ($keys) { |
|
| 446 | return in_array($key, $keys); |
||
| 447 | 1 | }, ARRAY_FILTER_USE_KEY); |
|
| 448 | |||
| 449 | 1 | $rels = $this->buildCollection($rels, $entityRels[$relationship])->setTotal(count($rels)); |
|
| 450 | 1 | $this->decorate($rels, StoreInterface::ON_READ); |
|
| 451 | |||
| 452 | 1 | $entity->addRelationship($relationship, $rels); |
|
| 453 | 1 | } |
|
| 454 | 1 | } |
|
| 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 | 1 | protected function getRelationshipBinds(Collection $collection, $relationship, $key) |
|
| 466 | { |
||
| 467 | 1 | $primaries = []; |
|
| 468 | |||
| 469 | 1 | foreach ($collection->getIterator() as $entity) { |
|
| 470 | 1 | if (! array_key_exists($relationship, $entity->getRelationshipMap())) { |
|
| 471 | continue; |
||
| 472 | } |
||
| 473 | |||
| 474 | 1 | $primaries[] = $entity[$key]; |
|
| 475 | 1 | } |
|
| 476 | |||
| 477 | 1 | return $primaries; |
|
| 478 | } |
||
| 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 | 2 | public function getRelationshipMap($relationship = null) |
|
| 491 | { |
||
| 492 | 2 | if (is_null($relationship)) { |
|
| 493 | 1 | return $this->relationships; |
|
| 494 | } |
||
| 495 | |||
| 496 | 2 | 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 | 2 | $map = $this->relationships[$relationship]; |
|
| 503 | |||
| 504 | foreach ([ |
||
| 505 | 2 | 'defined_in' => ['table', 'primary', 'entity'], |
|
| 506 | 2 | 'target' => ['table', 'primary', 'relationship'] |
|
| 507 | 2 | ] as $key => $value) { |
|
| 508 | 2 | 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 | 2 | 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 | 2 | } |
|
| 526 | |||
| 527 | 2 | 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 |