Conditions | 10 |
Paths | 15 |
Total Lines | 62 |
Code Lines | 35 |
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 |
||
108 | private function buildAggregation(array $identifiers, array $context, Builder $previousAggregationBuilder, int $remainingIdentifiers, Builder $topAggregationBuilder = null): Builder |
||
109 | { |
||
110 | if ($remainingIdentifiers <= 0) { |
||
111 | return $previousAggregationBuilder; |
||
112 | } |
||
113 | |||
114 | $topAggregationBuilder = $topAggregationBuilder ?? $previousAggregationBuilder; |
||
115 | |||
116 | [$identifier, $identifierResourceClass] = $context['identifiers'][$remainingIdentifiers - 1]; |
||
117 | $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property']; |
||
118 | |||
119 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
120 | |||
121 | if (!$manager instanceof DocumentManager) { |
||
122 | throw new RuntimeException("The manager for $identifierResourceClass must be a DocumentManager."); |
||
123 | } |
||
124 | |||
125 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
126 | |||
127 | if (!$classMetadata instanceof ClassMetadata) { |
||
|
|||
128 | throw new RuntimeException( |
||
129 | "The class metadata for $identifierResourceClass must be an instance of ClassMetadata." |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | $aggregation = $manager->createAggregationBuilder($identifierResourceClass); |
||
134 | $normalizedIdentifiers = []; |
||
135 | |||
136 | if (isset($identifiers[$identifier])) { |
||
137 | // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated |
||
138 | if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) { |
||
139 | $normalizedIdentifiers = $identifiers[$identifier]; |
||
140 | } else { |
||
141 | $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | if ($classMetadata->hasAssociation($previousAssociationProperty)) { |
||
146 | $aggregation->lookup($previousAssociationProperty)->alias($previousAssociationProperty); |
||
147 | foreach ($normalizedIdentifiers as $key => $value) { |
||
148 | $aggregation->match()->field($key)->equals($value); |
||
149 | } |
||
150 | } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) { |
||
151 | foreach ($normalizedIdentifiers as $key => $value) { |
||
152 | $aggregation->match()->field($key)->equals($value); |
||
153 | } |
||
154 | |||
155 | return $aggregation; |
||
156 | } |
||
157 | |||
158 | // Recurse aggregations |
||
159 | $aggregation = $this->buildAggregation($identifiers, $context, $aggregation, --$remainingIdentifiers, $topAggregationBuilder); |
||
160 | |||
161 | $results = $aggregation->execute()->toArray(); |
||
162 | $in = array_reduce($results, function ($in, $result) use ($previousAssociationProperty) { |
||
163 | return $in + array_map(function ($result) { |
||
164 | return $result['_id']; |
||
165 | }, $result[$previousAssociationProperty] ?? []); |
||
166 | }, []); |
||
167 | $previousAggregationBuilder->match()->field('_id')->in($in); |
||
168 | |||
169 | return $previousAggregationBuilder; |
||
170 | } |
||
172 |