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 |
||
106 | private function buildAggregation(array $identifiers, array $context, Builder $previousAggregationBuilder, int $remainingIdentifiers, Builder $topAggregationBuilder = null): Builder |
||
107 | { |
||
108 | if ($remainingIdentifiers <= 0) { |
||
109 | return $previousAggregationBuilder; |
||
110 | } |
||
111 | |||
112 | $topAggregationBuilder = $topAggregationBuilder ?? $previousAggregationBuilder; |
||
113 | |||
114 | list($identifier, $identifierResourceClass) = $context['identifiers'][$remainingIdentifiers - 1]; |
||
115 | $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property']; |
||
116 | |||
117 | $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass); |
||
118 | |||
119 | if (!$manager instanceof DocumentManager) { |
||
120 | throw new RuntimeException("The manager for $identifierResourceClass must be a DocumentManager."); |
||
121 | } |
||
122 | |||
123 | $classMetadata = $manager->getClassMetadata($identifierResourceClass); |
||
124 | |||
125 | if (!$classMetadata instanceof ClassMetadata) { |
||
|
|||
126 | throw new RuntimeException( |
||
127 | "The class metadata for $identifierResourceClass must be an instance of ClassMetadata." |
||
128 | ); |
||
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 |