Total Complexity | 88 |
Total Lines | 562 |
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 | public static function make(Orm $orm, MapperConfig $mapperConfig) |
||
121 | { |
||
122 | $mapper = new static($orm, $mapperConfig->entityHydrator); |
||
123 | $mapper->table = $mapperConfig->table; |
||
124 | $mapper->tableAlias = $mapperConfig->tableAlias; |
||
125 | $mapper->primaryKey = $mapperConfig->primaryKey; |
||
126 | $mapper->columns = $mapperConfig->columns; |
||
127 | $mapper->entityDefaultAttributes = $mapperConfig->entityDefaultAttributes; |
||
128 | $mapper->columnAttributeMap = $mapperConfig->columnAttributeMap; |
||
129 | $mapper->scopes = $mapperConfig->scopes; |
||
130 | $mapper->guards = $mapperConfig->guards; |
||
131 | $mapper->tableReference = QueryHelper::reference($mapper->table, $mapper->tableAlias); |
||
132 | |||
133 | if ($mapperConfig->relations) { |
||
134 | $mapper->relations = array_merge($mapper->relations, $mapperConfig->relations); |
||
135 | } |
||
136 | |||
137 | if ($mapperConfig->entityClass) { |
||
138 | $mapper->entityClass = $mapperConfig->entityClass; |
||
139 | } |
||
140 | |||
141 | if ($mapperConfig->behaviours && ! empty($mapperConfig->behaviours)) { |
||
142 | $mapper->use(...$mapperConfig->behaviours); |
||
143 | } |
||
144 | |||
145 | return $mapper; |
||
146 | } |
||
147 | |||
148 | public function __construct(Orm $orm, HydratorInterface $entityHydrator = null, QueryBuilder $queryBuilder = null) |
||
149 | { |
||
150 | $this->orm = $orm; |
||
151 | |||
152 | if (! $entityHydrator) { |
||
153 | $entityHydrator = new GenericEntityHydrator(); |
||
154 | $entityHydrator->setMapper($this); |
||
155 | $entityHydrator->setCastingManager($orm->getCastingManager()); |
||
156 | } |
||
157 | $this->entityHydrator = $entityHydrator; |
||
158 | |||
159 | if (! $queryBuilder) { |
||
160 | $queryBuilder = QueryBuilder::getInstance(); |
||
161 | } |
||
162 | $this->queryBuilder = $queryBuilder; |
||
163 | } |
||
164 | |||
165 | public function __call(string $method, array $params) |
||
166 | { |
||
167 | switch ($method) { |
||
168 | case 'where': |
||
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 |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getTableAlias($returnTableIfNull = false) |
||
276 | } |
||
277 | |||
278 | public function getTableReference() |
||
279 | { |
||
280 | if (!$this->tableReference) { |
||
281 | $this->tableReference = QueryHelper::reference($this->table, $this->tableAlias); |
||
282 | } |
||
283 | |||
284 | return $this->tableReference; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getColumns(): array |
||
291 | { |
||
292 | return $this->columns; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @return array |
||
297 | */ |
||
298 | public function getColumnAttributeMap(): array |
||
299 | { |
||
300 | return $this->columnAttributeMap; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getEntityClass(): string |
||
307 | { |
||
308 | return $this->entityClass; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return array |
||
313 | */ |
||
314 | public function getGuards(): array |
||
315 | { |
||
316 | return $this->guards; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param $data |
||
321 | * |
||
322 | * @return EntityInterface |
||
323 | */ |
||
324 | public function newEntity(array $data): EntityInterface |
||
325 | { |
||
326 | $entity = $this->entityHydrator->hydrate(array_merge($this->getEntityDefaults(), $data)); |
||
327 | |||
328 | return $this->applyBehaviours(__FUNCTION__, $entity); |
||
329 | } |
||
330 | |||
331 | public function extractFromEntity(EntityInterface $entity): array |
||
332 | { |
||
333 | $data = $this->entityHydrator->extract($entity); |
||
334 | |||
335 | return $this->applyBehaviours(__FUNCTION__, $data); |
||
336 | } |
||
337 | |||
338 | public function newEntityFromRow(array $data = null, array $load = [], Tracker $tracker = null) |
||
339 | { |
||
340 | if ($data == null) { |
||
341 | return null; |
||
342 | } |
||
343 | |||
344 | $receivedTracker = ! ! $tracker; |
||
345 | if (! $tracker) { |
||
346 | $receivedTracker = false; |
||
347 | $tracker = new Tracker($this, [$data]); |
||
348 | } |
||
349 | |||
350 | $entity = $this->newEntity($data); |
||
351 | $this->injectRelations($entity, $tracker, $load); |
||
352 | $entity->setPersistenceState(StateEnum::SYNCHRONIZED); |
||
353 | |||
354 | if (! $receivedTracker) { |
||
355 | $tracker->replaceRows([$entity]); |
||
356 | if ($tracker->isDisposable()) { |
||
357 | unset($tracker); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | return $entity; |
||
362 | } |
||
363 | |||
364 | public function newCollectionFromRows(array $rows, array $load = []): Collection |
||
365 | { |
||
366 | $entities = []; |
||
367 | $tracker = new Tracker($this, $rows); |
||
368 | foreach ($rows as $row) { |
||
369 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
370 | $entities[] = $entity; |
||
371 | } |
||
372 | $tracker->replaceRows($entities); |
||
373 | if ($tracker->isDisposable()) { |
||
374 | unset($tracker); |
||
375 | } |
||
376 | |||
377 | return new Collection($entities); |
||
378 | } |
||
379 | |||
380 | public function newPaginatedCollectionFromRows( |
||
381 | array $rows, |
||
382 | int $totalCount, |
||
383 | int $perPage, |
||
384 | int $currentPage, |
||
385 | array $load = [] |
||
386 | ): PaginatedCollection { |
||
387 | $entities = []; |
||
388 | $tracker = new Tracker($this, $rows); |
||
389 | foreach ($rows as $row) { |
||
390 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
391 | $entities[] = $entity; |
||
392 | } |
||
393 | $tracker->replaceRows($entities); |
||
394 | if ($tracker->isDisposable()) { |
||
395 | unset($tracker); |
||
396 | } |
||
397 | |||
398 | return new PaginatedCollection($entities, $totalCount, $perPage, $currentPage); |
||
399 | } |
||
400 | |||
401 | protected function injectRelations(EntityInterface $entity, Tracker $tracker, array $eagerLoad = []) |
||
402 | { |
||
403 | $trackerIdDisposable = true; |
||
404 | foreach (array_keys($this->relations) as $name) { |
||
405 | $relation = $this->getRelation($name); |
||
406 | $queryCallback = $eagerLoad[$name] ?? null; |
||
407 | $nextLoad = Arr::getChildren($eagerLoad, $name); |
||
408 | |||
409 | if (! $tracker->hasRelation($name)) { |
||
410 | $tracker->setRelation($name, $relation, $queryCallback); |
||
411 | } |
||
412 | |||
413 | if (array_key_exists($name, $eagerLoad) || $relation->isEagerLoad()) { |
||
414 | $relation->attachMatchesToEntity($entity, $tracker->getRelationResults($name)); |
||
415 | } elseif ($relation->isLazyLoad()) { |
||
416 | $trackerIdDisposable = false; |
||
417 | $relation->attachLazyValueToEntity($entity, $tracker); |
||
418 | } |
||
419 | } |
||
420 | |||
421 | $tracker->setDisposable($trackerIdDisposable); |
||
422 | } |
||
423 | |||
424 | protected function getEntityDefaults() |
||
425 | { |
||
426 | return $this->entityDefaultAttributes; |
||
427 | } |
||
428 | |||
429 | public function setEntityAttribute(EntityInterface $entity, $attribute, $value) |
||
430 | { |
||
431 | return $entity->set($attribute, $value); |
||
432 | } |
||
433 | |||
434 | public function getEntityAttribute(EntityInterface $entity, $attribute) |
||
435 | { |
||
436 | return $entity->get($attribute); |
||
437 | } |
||
438 | |||
439 | public function addRelation($name, $relation) |
||
440 | { |
||
441 | if (is_array($relation) || $relation instanceof Relation) { |
||
442 | $this->relations[$name] = $relation; |
||
443 | return; |
||
444 | } |
||
445 | throw new \InvalidArgumentException( |
||
446 | sprintf('The relation has to be an Relation instance or an array of configuration options') |
||
447 | ); |
||
448 | } |
||
449 | |||
450 | public function hasRelation($name): bool |
||
451 | { |
||
452 | return isset($this->relations[$name]); |
||
453 | } |
||
454 | |||
455 | public function getRelation($name): Relation |
||
456 | { |
||
457 | if (! $this->hasRelation($name)) { |
||
458 | throw new \InvalidArgumentException("Relation named {$name} is not registered for this mapper"); |
||
459 | } |
||
460 | |||
461 | if (is_array($this->relations[$name])) { |
||
462 | $this->relations[$name] = $this->orm->createRelation($this, $name, $this->relations[$name]); |
||
463 | } |
||
464 | $relation = $this->relations[$name]; |
||
465 | if (! $relation instanceof Relation) { |
||
466 | throw new \InvalidArgumentException("Relation named {$name} is not a proper Relation instance"); |
||
467 | } |
||
468 | |||
469 | return $relation; |
||
470 | } |
||
471 | |||
472 | public function getRelations(): array |
||
473 | { |
||
474 | return array_keys($this->relations); |
||
475 | } |
||
476 | |||
477 | public function newQuery(): Query |
||
478 | { |
||
479 | $query = $this->queryBuilder->newQuery($this); |
||
480 | |||
481 | return $this->applyBehaviours(__FUNCTION__, $query); |
||
482 | } |
||
483 | |||
484 | public function find($pk, array $load = []) |
||
485 | { |
||
486 | return $this->newQuery() |
||
487 | ->where($this->getPrimaryKey(), $pk) |
||
488 | ->load(...$load) |
||
489 | ->first(); |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * @param EntityInterface $entity |
||
494 | * |
||
495 | * @return bool |
||
496 | * @throws \Exception |
||
497 | */ |
||
498 | public function save(EntityInterface $entity, $withRelations = true) |
||
499 | { |
||
500 | $this->assertCanPersistEntity($entity); |
||
501 | $action = $this->newSaveAction($entity, ['relations' => $withRelations]); |
||
502 | |||
503 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
504 | $this->getWriteConnection()->beginTransaction(); |
||
505 | try { |
||
506 | $action->run(); |
||
507 | $this->getWriteConnection()->commit(); |
||
508 | $this->orm->getConnectionLocator()->lockToWrite(false); |
||
509 | return true; |
||
510 | } catch (\Exception $e) { |
||
511 | $this->getWriteConnection()->rollBack(); |
||
512 | $this->orm->getConnectionLocator()->lockToWrite(false); |
||
513 | throw $e; |
||
514 | } |
||
515 | } |
||
516 | |||
517 | public function newSaveAction(EntityInterface $entity, $options): BaseAction |
||
526 | } |
||
527 | |||
528 | public function delete(EntityInterface $entity, $withRelations = true) |
||
529 | { |
||
530 | $this->assertCanPersistEntity($entity); |
||
531 | |||
532 | $action = $this->newDeleteAction($entity, ['relations' => $withRelations]); |
||
533 | |||
534 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
535 | $this->getWriteConnection()->beginTransaction(); |
||
536 | try { |
||
537 | $action->run(); |
||
538 | $this->getWriteConnection()->commit(); |
||
539 | |||
540 | return true; |
||
541 | } catch (\Exception $e) { |
||
542 | $this->getWriteConnection()->rollBack(); |
||
543 | throw $e; |
||
544 | } |
||
545 | } |
||
546 | |||
547 | public function newDeleteAction(EntityInterface $entity, $options) |
||
548 | { |
||
549 | $action = new Delete($this, $entity, $options); |
||
550 | |||
551 | return $this->applyBehaviours('delete', $action); |
||
552 | } |
||
553 | |||
554 | protected function assertCanPersistEntity($entity) |
||
562 | )); |
||
563 | } |
||
564 | } |
||
565 | |||
566 | protected function applyBehaviours($target, $result, ...$args) |
||
567 | { |
||
568 | foreach ($this->behaviours as $behaviour) { |
||
569 | $method = 'on' . Helpers\Str::className($target); |
||
570 | if (method_exists($behaviour, $method)) { |
||
571 | $result = $behaviour->{$method}($this, $result, ...$args); |
||
572 | } |
||
573 | } |
||
574 | |||
575 | return $result; |
||
576 | } |
||
577 | |||
578 | public function getReadConnection() |
||
579 | { |
||
580 | return $this->orm->getConnectionLocator()->getRead(); |
||
581 | } |
||
582 | |||
583 | public function getWriteConnection() |
||
584 | { |
||
585 | return $this->orm->getConnectionLocator()->getWrite(); |
||
586 | } |
||
587 | |||
588 | public function getCasts() |
||
591 | } |
||
592 | } |
||
593 |
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