Total Complexity | 40 |
Total Lines | 210 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ElasticsearchEntityAggregatorHydrator 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 ElasticsearchEntityAggregatorHydrator, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
35 | class ElasticsearchEntityAggregatorHydrator extends AbstractElasticsearchAggregationHydrator |
||
36 | { |
||
37 | /** |
||
38 | * @var DefinitionInstanceRegistry |
||
39 | */ |
||
40 | private $definitionInstanceRegistry; |
||
41 | |||
42 | public function __construct( |
||
43 | DefinitionInstanceRegistry $definitionInstanceRegistry |
||
44 | ) { |
||
45 | $this->definitionInstanceRegistry = $definitionInstanceRegistry; |
||
46 | } |
||
47 | |||
48 | public function getDecorated(): AbstractElasticsearchAggregationHydrator |
||
49 | { |
||
50 | throw new DecorationPatternException(self::class); |
||
51 | } |
||
52 | |||
53 | public function hydrate(EntityDefinition $definition, Criteria $criteria, Context $context, array $result): AggregationResultCollection |
||
74 | } |
||
75 | |||
76 | private function hydrateAggregation(Aggregation $aggregation, array $result, Context $context): AggregationResult |
||
77 | { |
||
78 | switch (true) { |
||
79 | case $aggregation instanceof StatsAggregation: |
||
80 | return new StatsResult($aggregation->getName(), $result['min'], $result['max'], $result['avg'], $result['sum']); |
||
81 | |||
82 | case $aggregation instanceof AvgAggregation: |
||
83 | return new AvgResult($aggregation->getName(), $result['value']); |
||
84 | |||
85 | case $aggregation instanceof CountAggregation: |
||
86 | return new CountResult($aggregation->getName(), $result['value']); |
||
87 | |||
88 | case $aggregation instanceof EntityAggregation: |
||
89 | return $this->hydrateEntityAggregation($aggregation, $result, $context); |
||
90 | |||
91 | case $aggregation instanceof MaxAggregation: |
||
92 | return new MaxResult($aggregation->getName(), $result['value']); |
||
93 | |||
94 | case $aggregation instanceof MinAggregation: |
||
95 | return new MinResult($aggregation->getName(), $result['value']); |
||
96 | |||
97 | case $aggregation instanceof SumAggregation: |
||
98 | return new SumResult($aggregation->getName(), $result['value']); |
||
99 | |||
100 | case $aggregation instanceof FilterAggregation: |
||
101 | $nested = $aggregation->getAggregation(); |
||
102 | |||
103 | if (!$nested) { |
||
104 | throw new \RuntimeException(sprintf('Filter aggregation %s contains no nested aggregation.', $aggregation->getName())); |
||
105 | } |
||
106 | $nestedResult = $result; |
||
107 | if (isset($nestedResult[$aggregation->getName()])) { |
||
108 | $nestedResult = $nestedResult[$aggregation->getName()]; |
||
109 | } |
||
110 | |||
111 | if (isset($nestedResult[$nested->getName()])) { |
||
112 | $nestedResult = $nestedResult[$nested->getName()]; |
||
113 | } |
||
114 | |||
115 | return $this->hydrateAggregation($nested, $nestedResult, $context); |
||
116 | |||
117 | case $aggregation instanceof DateHistogramAggregation: |
||
118 | return $this->hydrateDateHistogram($aggregation, $result, $context); |
||
|
|||
119 | |||
120 | case $aggregation instanceof TermsAggregation: |
||
121 | return $this->hydrateTermsAggregation($aggregation, $result, $context); |
||
122 | |||
123 | default: |
||
124 | throw new \RuntimeException(sprintf('Provided aggregation of class %s is not supported', \get_class($aggregation))); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | private function hydrateEntityAggregation(EntityAggregation $aggregation, array $result, Context $context): EntityResult |
||
141 | } |
||
142 | |||
143 | private function hydrateDateHistogram(DateHistogramAggregation $aggregation, array $result, Context $context) |
||
144 | { |
||
145 | if (isset($result[$aggregation->getName()])) { |
||
146 | $result = $result[$aggregation->getName()]; |
||
147 | } |
||
148 | |||
149 | if (!isset($result['buckets'])) { |
||
150 | return null; |
||
151 | } |
||
152 | |||
153 | $buckets = []; |
||
154 | foreach ($result['buckets'] as $bucket) { |
||
155 | $nested = null; |
||
156 | |||
157 | $nestedAggregation = $aggregation->getAggregation(); |
||
158 | if ($nestedAggregation) { |
||
159 | $nested = $this->hydrateAggregation($nestedAggregation, $bucket[$nestedAggregation->getName()], $context); |
||
160 | } |
||
161 | |||
162 | $key = $bucket['key'][$aggregation->getName() . '.key']; |
||
163 | |||
164 | $date = new \DateTime($key); |
||
165 | |||
166 | if ($aggregation->getFormat()) { |
||
167 | $value = $date->format($aggregation->getFormat()); |
||
168 | } else { |
||
169 | $value = EntityAggregator::formatDate($aggregation->getInterval(), $date); |
||
170 | } |
||
171 | |||
172 | $buckets[] = new Bucket($value, $bucket['doc_count'], $nested); |
||
173 | } |
||
174 | |||
175 | return new DateHistogramResult($aggregation->getName(), $buckets); |
||
176 | } |
||
177 | |||
178 | private function hydrateTermsAggregation(TermsAggregation $aggregation, array $result, Context $context): ?TermsResult |
||
179 | { |
||
180 | if ($aggregation->getSorting()) { |
||
181 | return $this->hydrateSortedTermsAggregation($aggregation, $result, $context); |
||
182 | } |
||
183 | |||
184 | if (isset($result[$aggregation->getName()])) { |
||
185 | $result = $result[$aggregation->getName()]; |
||
186 | } |
||
187 | |||
188 | $key = $aggregation->getName() . '.key'; |
||
189 | if (isset($result[$key])) { |
||
190 | $result = $result[$key]; |
||
191 | } |
||
192 | |||
193 | if (!isset($result['buckets'])) { |
||
194 | return null; |
||
195 | } |
||
196 | |||
197 | $buckets = []; |
||
198 | foreach ($result['buckets'] as $bucket) { |
||
199 | $nested = null; |
||
200 | |||
201 | $nestedAggregation = $aggregation->getAggregation(); |
||
202 | if ($nestedAggregation) { |
||
203 | $nested = $this->hydrateAggregation( |
||
204 | $nestedAggregation, |
||
205 | $bucket[$nestedAggregation->getName()], |
||
206 | $context |
||
207 | ); |
||
208 | } |
||
209 | |||
210 | $buckets[] = new Bucket((string) $bucket['key'], $bucket['doc_count'], $nested); |
||
211 | } |
||
212 | |||
213 | return new TermsResult($aggregation->getName(), $buckets); |
||
214 | } |
||
215 | |||
216 | private function hydrateSortedTermsAggregation(TermsAggregation $aggregation, array $result, Context $context): ?TermsResult |
||
245 | } |
||
246 | } |
||
247 |