| Total Complexity | 83 |
| Total Lines | 546 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Mapper 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.
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 Mapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Mapper |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Name of the class/interface to be used to determine |
||
| 33 | * if this mapper can persist a specific entity |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $entityClass = GenericEntity::class; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string|array |
||
| 40 | */ |
||
| 41 | protected $primaryKey = 'id'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $table; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Used in queries like so: FROM table as tableAlias |
||
| 50 | * This is especially useful if you are using prefixed tables |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $tableAlias = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $tableReference; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Table columns |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $columns = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Column casts |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $casts = ['id' => 'int']; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Column aliases (table column => entity attribute) |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $columnAttributeMap = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var HydratorInterface |
||
| 80 | */ |
||
| 81 | protected $entityHydrator; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Default attributes |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $entityDefaultAttributes = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * List of behaviours to be attached to the mapper |
||
| 91 | * @var array[BehaviourInterface] |
||
| 92 | */ |
||
| 93 | protected $behaviours = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $relations = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $scopes = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $guards = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var QueryBuilder |
||
| 112 | */ |
||
| 113 | protected $queryBuilder; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Orm |
||
| 117 | */ |
||
| 118 | protected $orm; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var Query |
||
| 122 | */ |
||
| 123 | private $queryPrototype; |
||
| 124 | |||
| 125 | public static function make(Orm $orm, MapperConfig $mapperConfig) |
||
| 126 | { |
||
| 127 | $mapper = new static($orm, $mapperConfig->entityHydrator); |
||
| 128 | $mapper->table = $mapperConfig->table; |
||
| 129 | $mapper->tableAlias = $mapperConfig->tableAlias; |
||
| 130 | $mapper->primaryKey = $mapperConfig->primaryKey; |
||
| 131 | $mapper->columns = $mapperConfig->columns; |
||
| 132 | $mapper->entityDefaultAttributes = $mapperConfig->entityDefaultAttributes; |
||
| 133 | $mapper->columnAttributeMap = $mapperConfig->columnAttributeMap; |
||
| 134 | $mapper->scopes = $mapperConfig->scopes; |
||
| 135 | $mapper->guards = $mapperConfig->guards; |
||
| 136 | $mapper->tableReference = QueryHelper::reference($mapper->table, $mapper->tableAlias); |
||
| 137 | |||
| 138 | if ($mapperConfig->relations) { |
||
| 139 | $mapper->relations = array_merge($mapper->relations, $mapperConfig->relations); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($mapperConfig->entityClass) { |
||
| 143 | $mapper->entityClass = $mapperConfig->entityClass; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($mapperConfig->behaviours && ! empty($mapperConfig->behaviours)) { |
||
| 147 | $mapper->use(...$mapperConfig->behaviours); |
||
| 148 | } |
||
| 149 | |||
| 150 | return $mapper; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function __construct(Orm $orm, HydratorInterface $entityHydrator = null, QueryBuilder $queryBuilder = null) |
||
| 154 | { |
||
| 155 | $this->orm = $orm; |
||
| 156 | if (! $entityHydrator) { |
||
| 157 | $entityHydrator = new GenericEntityHydrator($orm, $this); |
||
| 158 | } |
||
| 159 | if (! $queryBuilder) { |
||
| 160 | $this->queryBuilder = QueryBuilder::getInstance(); |
||
| 161 | } |
||
| 162 | $this->entityHydrator = $entityHydrator; |
||
| 163 | $this->tableReference = QueryHelper::reference($this->table, $this->tableAlias); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function __call(string $method, array $params) |
||
| 167 | { |
||
| 168 | switch ($method) { |
||
| 169 | case 'where': |
||
| 170 | case 'columns': |
||
| 171 | case 'orderBy': |
||
| 172 | $query = $this->newQuery(); |
||
| 173 | |||
| 174 | return $query->{$method}(...$params); |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | throw new \BadMethodCallException('Unknown method {$method} for class ' . get_class($this)); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Add behaviours to the mapper |
||
| 183 | * |
||
| 184 | * @param mixed ...$behaviours |
||
| 185 | */ |
||
| 186 | public function use(...$behaviours) |
||
| 187 | { |
||
| 188 | if (empty($behaviours)) { |
||
| 189 | return; |
||
| 190 | } |
||
| 191 | foreach ($behaviours as $behaviour) { |
||
| 192 | /** @var $behaviour BehaviourInterface */ |
||
| 193 | if (isset($this->behaviours[$behaviour->getName()])) { |
||
| 194 | throw new \BadMethodCallException( |
||
| 195 | sprintf('Behaviour "%s" is already registered', $behaviour->getName()) |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | $this->behaviours[$behaviour->getName()] = $behaviour; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | public function without(...$behaviours) |
||
| 203 | { |
||
| 204 | if (empty($behaviours)) { |
||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | $mapper = clone $this; |
||
| 208 | foreach ($behaviours as $behaviour) { |
||
| 209 | unset($mapper->behaviours[$behaviour]); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $mapper; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function addQueryScope($scope, callable $callback) |
||
| 216 | { |
||
| 217 | $this->scopes[$scope] = $callback; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function getQueryScope($scope) |
||
| 221 | { |
||
| 222 | return $this->scopes[$scope] ?? null; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function registerCasts(CastingManager $castingManager) |
||
| 251 | }); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return array|string |
||
| 256 | */ |
||
| 257 | public function getPrimaryKey() |
||
| 258 | { |
||
| 259 | return $this->primaryKey; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getTable(): string |
||
| 266 | { |
||
| 267 | return $this->table; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getTableAlias($returnTableIfNull = false) |
||
| 274 | { |
||
| 275 | return (! $this->tableAlias && $returnTableIfNull) ? $this->table : $this->tableAlias; |
||
| 276 | } |
||
| 277 | |||
| 278 | public function getTableReference() |
||
| 279 | { |
||
| 280 | return $this->tableReference; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getColumns(): array |
||
| 287 | { |
||
| 288 | return $this->columns; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getColumnAttributeMap(): array |
||
| 295 | { |
||
| 296 | return $this->columnAttributeMap; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getEntityClass(): string |
||
| 303 | { |
||
| 304 | return $this->entityClass; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function getGuards(): array |
||
| 311 | { |
||
| 312 | return $this->guards; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param $data |
||
| 317 | * |
||
| 318 | * @return EntityInterface |
||
| 319 | */ |
||
| 320 | public function newEntity(array $data): EntityInterface |
||
| 321 | { |
||
| 322 | $entity = $this->entityHydrator->hydrate(array_merge($this->getEntityDefaults(), $data)); |
||
| 323 | |||
| 324 | return $this->applyBehaviours(__FUNCTION__, $entity); |
||
| 325 | } |
||
| 326 | |||
| 327 | public function extractFromEntity(EntityInterface $entity): array |
||
| 328 | { |
||
| 329 | $data = $this->entityHydrator->extract($entity); |
||
| 330 | |||
| 331 | return $this->applyBehaviours(__FUNCTION__, $data); |
||
| 332 | } |
||
| 333 | |||
| 334 | public function newEntityFromRow(array $data = null, array $load = [], Tracker $tracker = null) |
||
| 335 | { |
||
| 336 | if ($data == null) { |
||
| 337 | return null; |
||
| 338 | } |
||
| 339 | |||
| 340 | $receivedTracker = ! ! $tracker; |
||
| 341 | if (! $tracker) { |
||
| 342 | $receivedTracker = false; |
||
| 343 | $tracker = new Tracker($this, [$data]); |
||
| 344 | } |
||
| 345 | |||
| 346 | $entity = $this->newEntity($data); |
||
| 347 | $this->injectRelations($entity, $tracker, $load); |
||
| 348 | $entity->setPersistenceState(StateEnum::SYNCHRONIZED); |
||
| 349 | |||
| 350 | if (! $receivedTracker) { |
||
| 351 | $tracker->replaceRows([$entity]); |
||
| 352 | if ($tracker->isDisposable()) { |
||
| 353 | unset($tracker); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | return $entity; |
||
| 358 | } |
||
| 359 | |||
| 360 | public function newCollectionFromRows(array $rows, array $load = []): Collection |
||
| 361 | { |
||
| 362 | $entities = []; |
||
| 363 | $tracker = new Tracker($this, $rows); |
||
| 364 | foreach ($rows as $row) { |
||
| 365 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
| 366 | $entities[] = $entity; |
||
| 367 | } |
||
| 368 | $tracker->replaceRows($entities); |
||
| 369 | if ($tracker->isDisposable()) { |
||
| 370 | unset($tracker); |
||
| 371 | } |
||
| 372 | |||
| 373 | return new Collection($entities); |
||
| 374 | } |
||
| 375 | |||
| 376 | public function newPaginatedCollectionFromRows( |
||
| 377 | array $rows, |
||
| 378 | int $totalCount, |
||
| 379 | int $perPage, |
||
| 380 | int $currentPage, |
||
| 381 | array $load = [] |
||
| 382 | ): PaginatedCollection { |
||
| 383 | $entities = []; |
||
| 384 | $tracker = new Tracker($this, $rows); |
||
| 385 | foreach ($rows as $row) { |
||
| 386 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
| 387 | $entities[] = $entity; |
||
| 388 | } |
||
| 389 | $tracker->replaceRows($entities); |
||
| 390 | if ($tracker->isDisposable()) { |
||
| 391 | unset($tracker); |
||
| 392 | } |
||
| 393 | |||
| 394 | return new PaginatedCollection($entities, $totalCount, $perPage, $currentPage); |
||
| 395 | } |
||
| 396 | |||
| 397 | protected function injectRelations(EntityInterface $entity, Tracker $tracker, array $eagerLoad = []) |
||
| 398 | { |
||
| 399 | $trackerIdDisposable = true; |
||
| 400 | foreach (array_keys($this->relations) as $name) { |
||
| 401 | $relation = $this->getRelation($name); |
||
| 402 | $queryCallback = $eagerLoad[$name] ?? null; |
||
| 403 | $nextLoad = Arr::getChildren($eagerLoad, $name); |
||
| 404 | |||
| 405 | if (! $tracker->hasRelation($name)) { |
||
| 406 | $tracker->setRelation($name, $relation, $queryCallback); |
||
| 407 | } |
||
| 408 | |||
| 409 | if (array_key_exists($name, $eagerLoad) || $relation->isEagerLoad()) { |
||
| 410 | $relation->attachMatchesToEntity($entity, $tracker->getRelationResults($name)); |
||
| 411 | } elseif ($relation->isLazyLoad()) { |
||
| 412 | $trackerIdDisposable = false; |
||
| 413 | $relation->attachLazyValueToEntity($entity, $tracker); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | $tracker->setDisposable($trackerIdDisposable); |
||
| 418 | } |
||
| 419 | |||
| 420 | protected function getEntityDefaults() |
||
| 421 | { |
||
| 422 | return $this->entityDefaultAttributes; |
||
| 423 | } |
||
| 424 | |||
| 425 | public function setEntityAttribute(EntityInterface $entity, $attribute, $value) |
||
| 426 | { |
||
| 427 | return $entity->set($attribute, $value); |
||
| 428 | } |
||
| 429 | |||
| 430 | public function getEntityAttribute(EntityInterface $entity, $attribute) |
||
| 431 | { |
||
| 432 | return $entity->get($attribute); |
||
| 433 | } |
||
| 434 | |||
| 435 | public function hasRelation($name): bool |
||
| 436 | { |
||
| 437 | return isset($this->relations[$name]); |
||
| 438 | } |
||
| 439 | |||
| 440 | public function getRelation($name): Relation |
||
| 441 | { |
||
| 442 | if (! $this->hasRelation($name)) { |
||
| 443 | throw new \InvalidArgumentException("Relation named {$name} is not registered for this mapper"); |
||
| 444 | } |
||
| 445 | |||
| 446 | if (is_array($this->relations[$name])) { |
||
| 447 | $this->relations[$name] = $this->orm->createRelation($this, $name, $this->relations[$name]); |
||
| 448 | } |
||
| 449 | $relation = $this->relations[$name]; |
||
| 450 | if (! $relation instanceof Relation) { |
||
| 451 | throw new \InvalidArgumentException("Relation named {$name} is not a proper Relation instance"); |
||
| 452 | } |
||
| 453 | |||
| 454 | return $relation; |
||
| 455 | } |
||
| 456 | |||
| 457 | public function getRelations(): array |
||
| 458 | { |
||
| 459 | return array_keys($this->relations); |
||
| 460 | } |
||
| 461 | |||
| 462 | public function newQuery(): Query |
||
| 463 | { |
||
| 464 | $query = $this->queryBuilder->newQuery($this); |
||
| 465 | |||
| 466 | return $this->applyBehaviours(__FUNCTION__, $query); |
||
| 467 | } |
||
| 468 | |||
| 469 | public function find($pk, array $load = []) |
||
| 470 | { |
||
| 471 | return $this->newQuery() |
||
| 472 | ->where($this->getPrimaryKey(), $pk) |
||
| 473 | ->load(...$load) |
||
| 474 | ->first(); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param EntityInterface $entity |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | * @throws \Exception |
||
| 482 | */ |
||
| 483 | public function save(EntityInterface $entity, $withRelations = true) |
||
| 484 | { |
||
| 485 | $this->assertCanPersistEntity($entity); |
||
| 486 | $action = $this->newSaveAction($entity, ['relations' => $withRelations]); |
||
| 487 | |||
| 488 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
| 489 | $this->getWriteConnection()->beginTransaction(); |
||
| 490 | try { |
||
| 491 | $action->run(); |
||
| 492 | $this->getWriteConnection()->commit(); |
||
| 493 | |||
| 494 | return true; |
||
| 495 | } catch (\Exception $e) { |
||
| 496 | $this->getWriteConnection()->rollBack(); |
||
| 497 | throw $e; |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | public function newSaveAction(EntityInterface $entity, $options): BaseAction |
||
| 510 | } |
||
| 511 | |||
| 512 | public function delete(EntityInterface $entity, $withRelations = true) |
||
| 513 | { |
||
| 514 | $this->assertCanPersistEntity($entity); |
||
| 515 | |||
| 516 | $action = $this->newDeleteAction($entity, ['relations' => $withRelations]); |
||
| 517 | |||
| 518 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
| 519 | $this->getWriteConnection()->beginTransaction(); |
||
| 520 | try { |
||
| 521 | $action->run(); |
||
| 522 | $this->getWriteConnection()->commit(); |
||
| 523 | |||
| 524 | return true; |
||
| 525 | } catch (\Exception $e) { |
||
| 526 | $this->getWriteConnection()->rollBack(); |
||
| 527 | throw $e; |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | public function newDeleteAction(EntityInterface $entity, $options) |
||
| 532 | { |
||
| 533 | $action = new Delete($this, $entity, $options); |
||
| 534 | |||
| 535 | return $this->applyBehaviours('delete', $action); |
||
| 536 | } |
||
| 537 | |||
| 538 | protected function assertCanPersistEntity($entity) |
||
| 546 | )); |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | protected function applyBehaviours($target, $result, ...$args) |
||
| 551 | { |
||
| 552 | foreach ($this->behaviours as $behaviour) { |
||
| 553 | $method = 'on' . Helpers\Str::className($target); |
||
| 554 | if (method_exists($behaviour, $method)) { |
||
| 555 | $result = $behaviour->{$method}($this, $result, ...$args); |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | return $result; |
||
| 560 | } |
||
| 561 | |||
| 562 | public function getReadConnection() |
||
| 563 | { |
||
| 564 | return $this->orm->getConnectionLocator()->getRead(); |
||
| 565 | } |
||
| 566 | |||
| 567 | public function getWriteConnection() |
||
| 568 | { |
||
| 569 | return $this->orm->getConnectionLocator()->getWrite(); |
||
| 570 | } |
||
| 571 | |||
| 572 | public function getCasts() |
||
| 575 | } |
||
| 576 | } |
||
| 577 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths