Total Complexity | 59 |
Total Lines | 319 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like StructEncoder 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 StructEncoder, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
17 | #[Package('core')] |
||
18 | class StructEncoder |
||
19 | { |
||
20 | /** |
||
21 | * @var array<string, bool> |
||
22 | */ |
||
23 | private array $protections = []; |
||
24 | |||
25 | /** |
||
26 | * @internal |
||
27 | */ |
||
28 | public function __construct( |
||
29 | private readonly DefinitionRegistryChain $registry, |
||
30 | private readonly NormalizerInterface $serializer |
||
31 | ) { |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return array<mixed> |
||
36 | */ |
||
37 | public function encode(Struct $struct, ResponseFields $fields): array |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param array<mixed> $array |
||
50 | * |
||
51 | * @return array<mixed> |
||
52 | */ |
||
53 | private function loop(Struct $struct, ResponseFields $fields, array $array): array |
||
54 | { |
||
55 | $data = $array; |
||
56 | |||
57 | if ($struct instanceof AggregationResultCollection) { |
||
58 | $mapped = []; |
||
59 | foreach (\array_keys($struct->getElements()) as $index => $key) { |
||
60 | if (!isset($data[$index]) || !\is_array($data[$index])) { |
||
61 | throw new \RuntimeException(\sprintf('Can not find encoded aggregation %s for data index %s', $key, $index)); |
||
62 | } |
||
63 | |||
64 | $entity = $struct->get($key); |
||
65 | if (!$entity instanceof Struct) { |
||
66 | throw new \RuntimeException(\sprintf('Aggregation %s is not an struct', $key)); |
||
67 | } |
||
68 | |||
69 | $mapped[$key] = $this->encodeStruct($entity, $fields, $data[$index]); |
||
70 | } |
||
71 | |||
72 | return $mapped; |
||
73 | } |
||
74 | |||
75 | if ($struct instanceof EntitySearchResult) { |
||
76 | $data = $this->encodeStruct($struct, $fields, $data); |
||
77 | |||
78 | if (isset($data['elements'])) { |
||
79 | $entities = []; |
||
80 | |||
81 | foreach (\array_values($data['elements']) as $index => $value) { |
||
82 | $entity = $struct->getAt($index); |
||
83 | if (!$entity instanceof Struct) { |
||
84 | throw new \RuntimeException(\sprintf('Entity %s is not an struct', $index)); |
||
85 | } |
||
86 | |||
87 | $entities[] = $this->encodeStruct($entity, $fields, $value); |
||
88 | } |
||
89 | $data['elements'] = $entities; |
||
90 | } |
||
91 | |||
92 | return $data; |
||
93 | } |
||
94 | |||
95 | if ($struct instanceof ErrorCollection) { |
||
96 | return array_map(static fn (Error $error) => $error->jsonSerialize(), $struct->getElements()); |
||
97 | } |
||
98 | |||
99 | if ($struct instanceof Collection) { |
||
100 | $new = []; |
||
101 | foreach ($data as $index => $value) { |
||
102 | $new[] = $this->encodeStruct($struct->getAt($index), $fields, $value); |
||
|
|||
103 | } |
||
104 | |||
105 | return $new; |
||
106 | } |
||
107 | |||
108 | return $this->encodeStruct($struct, $fields, $data); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param array<mixed> $data |
||
113 | * |
||
114 | * @return array<mixed> |
||
115 | */ |
||
116 | private function encodeStruct(Struct $struct, ResponseFields $fields, array $data, ?string $alias = null): array |
||
117 | { |
||
118 | $alias = $alias ?? $struct->getApiAlias(); |
||
119 | |||
120 | foreach ($data as $property => $value) { |
||
121 | if ($property === 'customFields' && $value === []) { |
||
122 | $data[$property] = $value = new \stdClass(); |
||
123 | } |
||
124 | |||
125 | if ($property === 'extensions') { |
||
126 | $data[$property] = $this->encodeExtensions($struct, $fields, $value); |
||
127 | |||
128 | if (empty($data[$property])) { |
||
129 | unset($data[$property]); |
||
130 | } |
||
131 | |||
132 | continue; |
||
133 | } |
||
134 | |||
135 | if (!$this->isAllowed($alias, (string) $property, $fields) && !$fields->hasNested($alias, (string) $property)) { |
||
136 | unset($data[$property]); |
||
137 | |||
138 | continue; |
||
139 | } |
||
140 | |||
141 | if (!\is_array($value)) { |
||
142 | continue; |
||
143 | } |
||
144 | |||
145 | $object = $value; |
||
146 | if (\array_key_exists($property, $struct->getVars())) { |
||
147 | $object = $struct->getVars()[$property]; |
||
148 | } |
||
149 | |||
150 | if ($object instanceof Struct) { |
||
151 | $data[$property] = $this->loop($object, $fields, $value); |
||
152 | |||
153 | continue; |
||
154 | } |
||
155 | |||
156 | // simple array of structs case |
||
157 | if ($this->isStructArray($object)) { |
||
158 | $array = []; |
||
159 | foreach ($object as $key => $item) { |
||
160 | $array[$key] = $this->encodeStruct($item, $fields, $value[$key]); |
||
161 | } |
||
162 | |||
163 | $data[$property] = $array; |
||
164 | |||
165 | continue; |
||
166 | } |
||
167 | |||
168 | $data[$property] = $this->encodeNestedArray($struct->getApiAlias(), (string) $property, $value, $fields); |
||
169 | } |
||
170 | |||
171 | $data['apiAlias'] = $struct->getApiAlias(); |
||
172 | |||
173 | return $data; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param array<mixed> $data |
||
178 | * |
||
179 | * @return array<mixed> |
||
180 | */ |
||
181 | private function encodeNestedArray(string $alias, string $prefix, array $data, ResponseFields $fields): array |
||
182 | { |
||
183 | if ($prefix !== 'translated' && !$fields->hasNested($alias, $prefix)) { |
||
184 | return $data; |
||
185 | } |
||
186 | |||
187 | foreach ($data as $property => &$value) { |
||
188 | if ($property === 'customFields' && $value === []) { |
||
189 | $value = new \stdClass(); |
||
190 | } |
||
191 | |||
192 | $accessor = $prefix . '.' . $property; |
||
193 | if ($prefix === 'translated') { |
||
194 | $accessor = $property; |
||
195 | } |
||
196 | |||
197 | if (!$this->isAllowed($alias, $accessor, $fields)) { |
||
198 | unset($data[$property]); |
||
199 | |||
200 | continue; |
||
201 | } |
||
202 | |||
203 | if (!\is_array($value)) { |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | $data[$property] = $this->encodeNestedArray($alias, $accessor, $value, $fields); |
||
208 | } |
||
209 | |||
210 | unset($value); |
||
211 | |||
212 | return $data; |
||
213 | } |
||
214 | |||
215 | private function isAllowed(string $type, string $property, ResponseFields $fields): bool |
||
216 | { |
||
217 | if ($this->isProtected($type, $property)) { |
||
218 | return false; |
||
219 | } |
||
220 | |||
221 | return $fields->isAllowed($type, $property); |
||
222 | } |
||
223 | |||
224 | private function isProtected(string $type, string $property): bool |
||
225 | { |
||
226 | $key = $type . '.' . $property; |
||
227 | if (isset($this->protections[$key])) { |
||
228 | return $this->protections[$key]; |
||
229 | } |
||
230 | |||
231 | if (!$this->registry->has($type)) { |
||
232 | return $this->protections[$key] = false; |
||
233 | } |
||
234 | |||
235 | $definition = $this->registry->getByEntityName($type); |
||
236 | |||
237 | $field = $definition->getField($property); |
||
238 | |||
239 | if ($property === 'translated') { |
||
240 | return $this->protections[$key] = false; |
||
241 | } |
||
242 | |||
243 | if (!$field) { |
||
244 | return $this->protections[$key] = true; |
||
245 | } |
||
246 | |||
247 | /** @var ApiAware|null $flag */ |
||
248 | $flag = $field->getFlag(ApiAware::class); |
||
249 | |||
250 | if ($flag === null) { |
||
251 | return $this->protections[$key] = true; |
||
252 | } |
||
253 | |||
254 | if (!$flag->isSourceAllowed(SalesChannelApiSource::class)) { |
||
255 | return $this->protections[$key] = true; |
||
256 | } |
||
257 | |||
258 | return $this->protections[$key] = false; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param array<string, mixed> $value |
||
263 | * |
||
264 | * @return array<string, mixed> |
||
265 | */ |
||
266 | private function encodeExtensions(Struct $struct, ResponseFields $fields, array $value): array |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param array|mixed $object |
||
323 | */ |
||
324 | private function isStructArray($object): bool |
||
336 | } |
||
337 | } |
||
338 |