Total Complexity | 47 |
Total Lines | 221 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ResolveCollectionFactory 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 ResolveCollectionFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ResolveCollectionFactory |
||
25 | { |
||
26 | public function __construct( |
||
27 | protected EntityManager $entityManager, |
||
28 | protected Config $config, |
||
29 | protected FieldResolver $fieldResolver, |
||
30 | protected TypeManager $typeManager, |
||
31 | ) { |
||
32 | } |
||
33 | |||
34 | public function parseValue(ClassMetadata $metadata, string $field, mixed $value): mixed |
||
35 | { |
||
36 | /** @psalm-suppress UndefinedDocblockClass */ |
||
37 | $fieldMapping = $metadata->getFieldMapping($field); |
||
38 | $graphQLType = $this->typeManager->get($fieldMapping['type']); |
||
39 | |||
40 | return $graphQLType->parseValue($graphQLType->serialize($value)); |
||
41 | } |
||
42 | |||
43 | /** @param mixed[] $value */ |
||
44 | public function parseArrayValue(ClassMetadata $metadata, string $field, array $value): mixed |
||
45 | { |
||
46 | foreach ($value as $key => $val) { |
||
47 | $value[$key] = $this->parseValue($metadata, $field, $val); |
||
48 | } |
||
49 | |||
50 | return $value; |
||
51 | } |
||
52 | |||
53 | public function get(Entity $entity): Closure |
||
54 | { |
||
55 | return function ($source, $args, $context, ResolveInfo $resolveInfo) { |
||
56 | $filter = $args['filter'] ?? []; |
||
57 | |||
58 | $fieldResolver = $this->fieldResolver; |
||
59 | $collection = $fieldResolver($source, $args, $context, $resolveInfo); |
||
60 | |||
61 | $collectionMetadata = $this->entityManager->getMetadataFactory() |
||
62 | ->getMetadataFor( |
||
63 | $this->entityManager->getMetadataFactory() |
||
1 ignored issue
–
show
|
|||
64 | ->getMetadataFor(ClassUtils::getRealClass($source::class)) |
||
65 | ->getAssociationTargetClass($resolveInfo->fieldName), |
||
66 | ); |
||
67 | |||
68 | return $this->buildPagination( |
||
69 | $filter, |
||
70 | $collection, |
||
71 | $this->buildCriteria($filter, $collectionMetadata), |
||
72 | ); |
||
73 | }; |
||
74 | } |
||
75 | |||
76 | /** @param mixed[] $filter */ |
||
77 | private function buildCriteria(array $filter, ClassMetadata $collectionMetadata): Criteria |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param mixed[] $filter |
||
148 | * |
||
149 | * @return mixed[] |
||
150 | */ |
||
151 | private function buildPagination(array $filter, PersistentCollection $collection, Criteria $criteria): array |
||
245 | ], |
||
246 | ]; |
||
249 |