Total Complexity | 81 |
Total Lines | 533 |
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 aliases (table column => entity attribute) |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $columnAttributeMap = []; |
||
71 | |||
72 | /** |
||
73 | * @var HydratorInterface |
||
74 | */ |
||
75 | protected $entityHydrator; |
||
76 | |||
77 | /** |
||
78 | * Default attributes |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $entityDefaultAttributes = []; |
||
82 | |||
83 | /** |
||
84 | * List of behaviours to be attached to the mapper |
||
85 | * @var array[BehaviourInterface] |
||
86 | */ |
||
87 | protected $behaviours = []; |
||
88 | |||
89 | /** |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $relations = []; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $scopes = []; |
||
98 | |||
99 | /** |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $guards = []; |
||
103 | |||
104 | /** |
||
105 | * @var QueryBuilder |
||
106 | */ |
||
107 | protected $queryBuilder; |
||
108 | |||
109 | /** |
||
110 | * @var Orm |
||
111 | */ |
||
112 | protected $orm; |
||
113 | |||
114 | /** |
||
115 | * @var Query |
||
116 | */ |
||
117 | private $queryPrototype; |
||
118 | |||
119 | public static function make(Orm $orm, MapperConfig $mapperConfig) |
||
120 | { |
||
121 | $mapper = new static($orm, $mapperConfig->entityHydrator); |
||
122 | $mapper->table = $mapperConfig->table; |
||
123 | $mapper->tableAlias = $mapperConfig->tableAlias; |
||
124 | $mapper->primaryKey = $mapperConfig->primaryKey; |
||
125 | $mapper->columns = $mapperConfig->columns; |
||
126 | $mapper->entityDefaultAttributes = $mapperConfig->entityDefaultAttributes; |
||
127 | $mapper->columnAttributeMap = $mapperConfig->columnAttributeMap; |
||
128 | $mapper->scopes = $mapperConfig->scopes; |
||
129 | $mapper->guards = $mapperConfig->guards; |
||
130 | $mapper->tableReference = QueryHelper::reference($mapper->table, $mapper->tableAlias); |
||
131 | |||
132 | if ($mapperConfig->relations) { |
||
133 | $mapper->relations = array_merge($mapper->relations, $mapperConfig->relations); |
||
134 | } |
||
135 | |||
136 | if ($mapperConfig->entityClass) { |
||
137 | $mapper->entityClass = $mapperConfig->entityClass; |
||
138 | } |
||
139 | |||
140 | if ($mapperConfig->behaviours && ! empty($mapperConfig->behaviours)) { |
||
141 | $mapper->use(...$mapperConfig->behaviours); |
||
142 | } |
||
143 | |||
144 | return $mapper; |
||
145 | } |
||
146 | |||
147 | public function __construct(Orm $orm, HydratorInterface $entityHydrator = null, QueryBuilder $queryBuilder = null) |
||
148 | { |
||
149 | $this->orm = $orm; |
||
150 | if (! $entityHydrator) { |
||
151 | $entityHydrator = new GenericEntityHydrator($orm, $this); |
||
152 | } |
||
153 | if (! $queryBuilder) { |
||
154 | $this->queryBuilder = QueryBuilder::getInstance(); |
||
155 | } |
||
156 | $this->entityHydrator = $entityHydrator; |
||
157 | $this->tableReference = QueryHelper::reference($this->table, $this->tableAlias); |
||
158 | } |
||
159 | |||
160 | public function __call(string $method, array $params) |
||
161 | { |
||
162 | switch ($method) { |
||
163 | case 'where': |
||
164 | case 'columns': |
||
165 | case 'orderBy': |
||
166 | $query = $this->newQuery(); |
||
167 | |||
168 | return $query->{$method}(...$params); |
||
169 | } |
||
170 | |||
171 | |||
172 | throw new \BadMethodCallException('Unknown method {$method} for class ' . get_class($this)); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Add behaviours to the mapper |
||
177 | * |
||
178 | * @param mixed ...$behaviours |
||
179 | */ |
||
180 | public function use(...$behaviours) |
||
181 | { |
||
182 | if (empty($behaviours)) { |
||
183 | return; |
||
184 | } |
||
185 | foreach ($behaviours as $behaviour) { |
||
186 | /** @var $behaviour BehaviourInterface */ |
||
187 | if (isset($this->behaviours[$behaviour->getName()])) { |
||
188 | throw new \BadMethodCallException( |
||
189 | sprintf('Behaviour "%s" is already registered', $behaviour->getName()) |
||
190 | ); |
||
191 | } |
||
192 | $this->behaviours[$behaviour->getName()] = $behaviour; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | public function without(...$behaviours) |
||
197 | { |
||
198 | if (empty($behaviours)) { |
||
199 | return $this; |
||
200 | } |
||
201 | $mapper = clone $this; |
||
202 | foreach ($behaviours as $behaviour) { |
||
203 | unset($mapper->behaviours[$behaviour]); |
||
204 | } |
||
205 | |||
206 | return $mapper; |
||
207 | } |
||
208 | |||
209 | public function addQueryScope($scope, callable $callback) |
||
210 | { |
||
211 | $this->scopes[$scope] = $callback; |
||
212 | } |
||
213 | |||
214 | public function getQueryScope($scope) |
||
215 | { |
||
216 | return $this->scopes[$scope] ?? null; |
||
217 | } |
||
218 | |||
219 | public function registerCasts(CastingManager $castingManager) |
||
243 | }); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return array|string |
||
248 | */ |
||
249 | public function getPrimaryKey() |
||
250 | { |
||
251 | return $this->primaryKey; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getTable(): string |
||
258 | { |
||
259 | return $this->table; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getTableAlias($returnTableIfNull = false) |
||
266 | { |
||
267 | return (! $this->tableAlias && $returnTableIfNull) ? $this->table : $this->tableAlias; |
||
268 | } |
||
269 | |||
270 | public function getTableReference() |
||
271 | { |
||
272 | return $this->tableReference; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getColumns(): array |
||
279 | { |
||
280 | return $this->columns; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getColumnAttributeMap(): array |
||
287 | { |
||
288 | return $this->columnAttributeMap; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getEntityClass(): string |
||
295 | { |
||
296 | return $this->entityClass; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getGuards(): array |
||
303 | { |
||
304 | return $this->guards; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param $data |
||
309 | * |
||
310 | * @return EntityInterface |
||
311 | */ |
||
312 | public function newEntity(array $data): EntityInterface |
||
313 | { |
||
314 | $entity = $this->entityHydrator->hydrate(array_merge($this->getEntityDefaults(), $data)); |
||
315 | |||
316 | return $this->applyBehaviours(__FUNCTION__, $entity); |
||
317 | } |
||
318 | |||
319 | public function extractFromEntity(EntityInterface $entity): array |
||
320 | { |
||
321 | $data = $this->entityHydrator->extract($entity); |
||
322 | |||
323 | return $this->applyBehaviours(__FUNCTION__, $data); |
||
324 | } |
||
325 | |||
326 | public function newEntityFromRow(array $data = null, array $load = [], Tracker $tracker = null) |
||
327 | { |
||
328 | if ($data == null) { |
||
329 | return null; |
||
330 | } |
||
331 | |||
332 | $receivedTracker = ! ! $tracker; |
||
333 | if (! $tracker) { |
||
334 | $receivedTracker = false; |
||
335 | $tracker = new Tracker($this, [$data]); |
||
336 | } |
||
337 | |||
338 | $entity = $this->newEntity($data); |
||
339 | $this->injectRelations($entity, $tracker, $load); |
||
340 | $entity->setPersistenceState(StateEnum::SYNCHRONIZED); |
||
341 | |||
342 | if (! $receivedTracker) { |
||
343 | $tracker->replaceRows([$entity]); |
||
344 | if ($tracker->isDisposable()) { |
||
345 | unset($tracker); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | return $entity; |
||
350 | } |
||
351 | |||
352 | public function newCollectionFromRows(array $rows, array $load = []): Collection |
||
353 | { |
||
354 | $entities = []; |
||
355 | $tracker = new Tracker($this, $rows); |
||
356 | foreach ($rows as $row) { |
||
357 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
358 | $entities[] = $entity; |
||
359 | } |
||
360 | $tracker->replaceRows($entities); |
||
361 | if ($tracker->isDisposable()) { |
||
362 | unset($tracker); |
||
363 | } |
||
364 | |||
365 | return new Collection($entities); |
||
366 | } |
||
367 | |||
368 | public function newPaginatedCollectionFromRows( |
||
369 | array $rows, |
||
370 | int $totalCount, |
||
371 | int $perPage, |
||
372 | int $currentPage, |
||
373 | array $load = [] |
||
374 | ): PaginatedCollection { |
||
375 | $entities = []; |
||
376 | $tracker = new Tracker($this, $rows); |
||
377 | foreach ($rows as $row) { |
||
378 | $entity = $this->newEntityFromRow($row, $load, $tracker); |
||
379 | $entities[] = $entity; |
||
380 | } |
||
381 | $tracker->replaceRows($entities); |
||
382 | if ($tracker->isDisposable()) { |
||
383 | unset($tracker); |
||
384 | } |
||
385 | |||
386 | return new PaginatedCollection($entities, $totalCount, $perPage, $currentPage); |
||
387 | } |
||
388 | |||
389 | protected function injectRelations(EntityInterface $entity, Tracker $tracker, array $eagerLoad = []) |
||
390 | { |
||
391 | $trackerIdDisposable = true; |
||
392 | foreach (array_keys($this->relations) as $name) { |
||
393 | $relation = $this->getRelation($name); |
||
394 | $queryCallback = $eagerLoad[$name] ?? null; |
||
395 | $nextLoad = Arr::getChildren($eagerLoad, $name); |
||
396 | |||
397 | if (! $tracker->hasRelation($name)) { |
||
398 | $tracker->setRelation($name, $relation, $queryCallback); |
||
399 | } |
||
400 | |||
401 | if (array_key_exists($name, $eagerLoad) || $relation->isEagerLoad()) { |
||
402 | $relation->attachMatchesToEntity($entity, $tracker->getRelationResults($name)); |
||
403 | } elseif ($relation->isLazyLoad()) { |
||
404 | $trackerIdDisposable = false; |
||
405 | $relation->attachLazyValueToEntity($entity, $tracker); |
||
406 | } |
||
407 | } |
||
408 | |||
409 | $tracker->setDisposable($trackerIdDisposable); |
||
410 | } |
||
411 | |||
412 | protected function getEntityDefaults() |
||
413 | { |
||
414 | return $this->entityDefaultAttributes; |
||
415 | } |
||
416 | |||
417 | public function setEntityAttribute(EntityInterface $entity, $attribute, $value) |
||
418 | { |
||
419 | return $entity->set($attribute, $value); |
||
420 | } |
||
421 | |||
422 | public function getEntityAttribute(EntityInterface $entity, $attribute) |
||
423 | { |
||
424 | return $entity->get($attribute); |
||
425 | } |
||
426 | |||
427 | public function hasRelation($name): bool |
||
428 | { |
||
429 | return isset($this->relations[$name]); |
||
430 | } |
||
431 | |||
432 | public function getRelation($name): Relation |
||
433 | { |
||
434 | if (! $this->hasRelation($name)) { |
||
435 | throw new \InvalidArgumentException("Relation named {$name} is not registered for this mapper"); |
||
436 | } |
||
437 | |||
438 | if (is_array($this->relations[$name])) { |
||
439 | $this->relations[$name] = $this->orm->createRelation($this, $name, $this->relations[$name]); |
||
440 | } |
||
441 | $relation = $this->relations[$name]; |
||
442 | if (! $relation instanceof Relation) { |
||
443 | throw new \InvalidArgumentException("Relation named {$name} is not a proper Relation instance"); |
||
444 | } |
||
445 | |||
446 | return $relation; |
||
447 | } |
||
448 | |||
449 | public function getRelations(): array |
||
450 | { |
||
451 | return array_keys($this->relations); |
||
452 | } |
||
453 | |||
454 | public function newQuery(): Query |
||
455 | { |
||
456 | $query = $this->queryBuilder->newQuery($this); |
||
457 | |||
458 | return $this->applyBehaviours(__FUNCTION__, $query); |
||
459 | } |
||
460 | |||
461 | public function find($pk, array $load = []) |
||
462 | { |
||
463 | return $this->newQuery() |
||
464 | ->where($this->getPrimaryKey(), $pk) |
||
465 | ->load(...$load) |
||
466 | ->first(); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @param EntityInterface $entity |
||
471 | * |
||
472 | * @return bool |
||
473 | * @throws \Exception |
||
474 | */ |
||
475 | public function save(EntityInterface $entity, $withRelations = true) |
||
476 | { |
||
477 | $this->assertCanPersistEntity($entity); |
||
478 | $action = $this->newSaveAction($entity, ['relations' => $withRelations]); |
||
479 | |||
480 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
481 | $this->getWriteConnection()->beginTransaction(); |
||
482 | try { |
||
483 | $action->run(); |
||
484 | $this->getWriteConnection()->commit(); |
||
485 | |||
486 | return true; |
||
487 | } catch (\Exception $e) { |
||
488 | $this->getWriteConnection()->rollBack(); |
||
489 | throw $e; |
||
490 | } |
||
491 | } |
||
492 | |||
493 | public function newSaveAction(EntityInterface $entity, $options): BaseAction |
||
502 | } |
||
503 | |||
504 | public function delete(EntityInterface $entity, $withRelations = true) |
||
505 | { |
||
506 | $this->assertCanPersistEntity($entity); |
||
507 | |||
508 | $action = $this->newDeleteAction($entity, ['relations' => $withRelations]); |
||
509 | |||
510 | $this->orm->getConnectionLocator()->lockToWrite(true); |
||
511 | $this->getWriteConnection()->beginTransaction(); |
||
512 | try { |
||
513 | $action->run(); |
||
514 | $this->getWriteConnection()->commit(); |
||
515 | |||
516 | return true; |
||
517 | } catch (\Exception $e) { |
||
518 | $this->getWriteConnection()->rollBack(); |
||
519 | throw $e; |
||
520 | } |
||
521 | } |
||
522 | |||
523 | public function newDeleteAction(EntityInterface $entity, $options) |
||
524 | { |
||
525 | $action = new Delete($this, $entity, $options); |
||
526 | |||
527 | return $this->applyBehaviours('delete', $action); |
||
528 | } |
||
529 | |||
530 | protected function assertCanPersistEntity($entity) |
||
538 | )); |
||
539 | } |
||
540 | } |
||
541 | |||
542 | protected function applyBehaviours($target, $result, ...$args) |
||
543 | { |
||
544 | foreach ($this->behaviours as $behaviour) { |
||
545 | $method = 'on' . Helpers\Str::className($target); |
||
546 | if (method_exists($behaviour, $method)) { |
||
547 | $result = $behaviour->{$method}($this, $result, ...$args); |
||
548 | } |
||
549 | } |
||
550 | |||
551 | return $result; |
||
552 | } |
||
553 | |||
554 | public function getReadConnection() |
||
557 | } |
||
558 | |||
559 | public function getWriteConnection() |
||
560 | { |
||
561 | return $this->orm->getConnectionLocator()->getWrite(); |
||
562 | } |
||
563 | } |
||
564 |
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