Conditions | 10 |
Paths | 15 |
Total Lines | 59 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
109 | private function buildAggregation(array $identifiers, array $context, Builder $previousAggregationBuilder, int $remainingIdentifiers, Builder $topAggregationBuilder = null): Builder |
||
110 | { |
||
111 | if ($remainingIdentifiers <= 0) { |
||
112 | return $previousAggregationBuilder; |
||
113 | } |
||
114 | |||
115 | $topAggregationBuilder = $topAggregationBuilder ?? $previousAggregationBuilder; |
||
116 | |||
117 | [$identifier, $identifierResourceClass] = $context['identifiers'][$remainingIdentifiers - 1]; |
||
118 | $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property']; |
||
119 | |||
120 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
121 | if (!$manager instanceof DocumentManager) { |
||
122 | throw new RuntimeException(sprintf('The manager for "%s" must be an instance of "%s".', $identifierResourceClass, DocumentManager::class)); |
||
123 | } |
||
124 | |||
125 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
126 | |||
127 | if (!$classMetadata instanceof ClassMetadata) { |
||
|
|||
128 | throw new RuntimeException(sprintf('The class metadata for "%s" must be an instance of "%s".', $identifierResourceClass, ClassMetadata::class)); |
||
129 | } |
||
130 | |||
131 | $aggregation = $manager->createAggregationBuilder($identifierResourceClass); |
||
132 | $normalizedIdentifiers = []; |
||
133 | |||
134 | if (isset($identifiers[$identifier])) { |
||
135 | // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated |
||
136 | if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) { |
||
137 | $normalizedIdentifiers = $identifiers[$identifier]; |
||
138 | } else { |
||
139 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | if ($classMetadata->hasAssociation($previousAssociationProperty)) { |
||
144 | $aggregation->lookup($previousAssociationProperty)->alias($previousAssociationProperty); |
||
145 | foreach ($normalizedIdentifiers as $key => $value) { |
||
146 | $aggregation->match()->field($key)->equals($value); |
||
147 | } |
||
148 | } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) { |
||
149 | foreach ($normalizedIdentifiers as $key => $value) { |
||
150 | $aggregation->match()->field($key)->equals($value); |
||
151 | } |
||
152 | |||
153 | return $aggregation; |
||
154 | } |
||
155 | |||
156 | // Recurse aggregations |
||
157 | $aggregation = $this->buildAggregation($identifiers, $context, $aggregation, --$remainingIdentifiers, $topAggregationBuilder); |
||
158 | |||
159 | $results = $aggregation->execute()->toArray(); |
||
160 | $in = array_reduce($results, function ($in, $result) use ($previousAssociationProperty) { |
||
161 | return $in + array_map(function ($result) { |
||
162 | return $result['_id']; |
||
163 | }, $result[$previousAssociationProperty] ?? []); |
||
164 | }, []); |
||
165 | $previousAggregationBuilder->match()->field('_id')->in($in); |
||
166 | |||
167 | return $previousAggregationBuilder; |
||
168 | } |
||
170 |