Passed
Push — master ( bd2ead...c0de24 )
by Kévin
03:06
created

ItemNormalizer   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 374
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 64
dl 0
loc 374
rs 3.4883
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 2
A __construct() 0 5 1
A getAttributes() 0 3 1
B normalize() 0 43 6
A setAttributeValue() 0 3 3
A isAllowedAttribute() 0 3 2
A supportsDenormalization() 0 3 2
B denormalize() 0 25 5
B denormalizeRelation() 0 17 5
B normalizeRelation() 0 28 6
C getComponents() 0 55 13
C getPopulatedRelations() 0 45 8
C getRelatedResources() 0 35 8
A getJsonApiCacheKey() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like ItemNormalizer 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 ItemNormalizer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\JsonApi\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\Api\OperationType;
18
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
19
use ApiPlatform\Core\Exception\InvalidArgumentException;
20
use ApiPlatform\Core\Exception\ItemNotFoundException;
21
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
24
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
25
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
26
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
28
29
/**
30
 * Converts between objects and array.
31
 *
32
 * @author Kévin Dunglas <[email protected]>
33
 * @author Amrouche Hamza <[email protected]>
34
 * @author Baptiste Meyer <[email protected]>
35
 */
36
final class ItemNormalizer extends AbstractItemNormalizer
37
{
38
    const FORMAT = 'jsonapi';
39
40
    private $componentsCache = [];
41
    private $resourceMetadataFactory;
42
43
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ResourceMetadataFactoryInterface $resourceMetadataFactory)
44
    {
45
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter);
46
47
        $this->resourceMetadataFactory = $resourceMetadataFactory;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function supportsNormalization($data, $format = null)
54
    {
55
        return self::FORMAT === $format && parent::supportsNormalization($data, $format);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function normalize($object, $format = null, array $context = [])
62
    {
63
        if (!isset($context['cache_key'])) {
64
            $context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
65
        }
66
67
        // Get and populate attributes data
68
        $objectAttributesData = parent::normalize($object, $format, $context);
69
70
        if (!\is_array($objectAttributesData)) {
71
            return $objectAttributesData;
72
        }
73
74
        // Get and populate item type
75
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
76
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
77
78
        // Get and populate relations
79
        $components = $this->getComponents($object, $format, $context);
80
        $populatedRelationContext = $context;
81
        $objectRelationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $components['relationships']);
82
        $objectRelatedResources = $this->getRelatedResources($object, $format, $context, $components['relationships']);
83
84
        $item = [
85
            'id' => $this->iriConverter->getIriFromItem($object),
86
            'type' => $resourceMetadata->getShortName(),
87
        ];
88
89
        if ($objectAttributesData) {
90
            $item['attributes'] = $objectAttributesData;
91
        }
92
93
        if ($objectRelationshipsData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $objectRelationshipsData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
94
            $item['relationships'] = $objectRelationshipsData;
95
        }
96
97
        $data = ['data' => $item];
98
99
        if ($objectRelatedResources) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $objectRelatedResources of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
100
            $data['included'] = $objectRelatedResources;
101
        }
102
103
        return $data;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function supportsDenormalization($data, $type, $format = null)
110
    {
111
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function denormalize($data, $class, $format = null, array $context = [])
118
    {
119
        // Avoid issues with proxies if we populated the object
120
        if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) {
121
            if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
122
                throw new InvalidArgumentException('Update is not allowed for this operation.');
123
            }
124
125
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri(
126
                $data['data']['id'],
127
                $context + ['fetch_data' => false]
128
            );
129
        }
130
131
        // Merge attributes and relations previous to apply parents denormalizing
132
        $dataToDenormalize = array_merge(
133
            $data['data']['attributes'] ?? [],
134
            $data['data']['relationships'] ?? []
135
        );
136
137
        return parent::denormalize(
138
            $dataToDenormalize,
139
            $class,
140
            $format,
141
            $context
142
        );
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected function getAttributes($object, $format = null, array $context)
149
    {
150
        return $this->getComponents($object, $format, $context)['attributes'];
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
157
    {
158
        parent::setAttributeValue($object, $attribute, \is_array($value) && array_key_exists('data', $value) ? $value['data'] : $value, $format, $context);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     *
164
     * @see http://jsonapi.org/format/#document-resource-object-linkage
165
     */
166
    protected function denormalizeRelation(string $attributeName, PropertyMetadata $propertyMetadata, string $className, $value, string $format = null, array $context)
167
    {
168
        // Give a chance to other normalizers (e.g.: DateTimeNormalizer)
169
        if (!$this->resourceClassResolver->isResourceClass($className)) {
170
            $context['resource_class'] = $className;
171
172
            return $this->serializer->denormalize($value, $className, $format, $context);
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of Symfony\Component\Serializer\SerializerInterface such as Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
            return $this->serializer->/** @scrutinizer ignore-call */ denormalize($value, $className, $format, $context);
Loading history...
173
        }
174
175
        if (!\is_array($value) || !isset($value['id'], $value['type'])) {
176
            throw new InvalidArgumentException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.');
177
        }
178
179
        try {
180
            return $this->iriConverter->getItemFromIri($value['id'], $context + ['fetch_data' => true]);
181
        } catch (ItemNotFoundException $e) {
182
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
183
        }
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     *
189
     * @see http://jsonapi.org/format/#document-resource-object-linkage
190
     */
191
    protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, string $format = null, array $context)
192
    {
193
        if (null === $relatedObject) {
194
            if (isset($context['operation_type'], $context['subresource_resources'][$resourceClass]) && OperationType::SUBRESOURCE === $context['operation_type']) {
195
                $iri = $this->iriConverter->getItemIriFromResourceClass($resourceClass, $context['subresource_resources'][$resourceClass]);
196
            } else {
197
                unset($context['resource_class']);
198
199
                return $this->serializer->normalize($relatedObject, $format, $context);
200
            }
201
        } else {
202
            $iri = $this->iriConverter->getIriFromItem($relatedObject);
203
204
            if (isset($context['resources'])) {
205
                $context['resources'][$iri] = $iri;
206
            }
207
            if (isset($context['api_included'])) {
208
                $context['api_sub_level'] = true;
209
                $data = $this->serializer->normalize($relatedObject, $format, $context);
210
                unset($context['api_sub_level']);
211
212
                return $data;
213
            }
214
        }
215
216
        return ['data' => [
217
            'type' => $this->resourceMetadataFactory->create($resourceClass)->getShortName(),
218
            'id' => $iri,
219
        ]];
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
226
    {
227
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
228
    }
229
230
    /**
231
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
232
     *
233
     * @param object      $object
234
     * @param string|null $format
235
     * @param array       $context
236
     *
237
     * @return array
238
     */
239
    private function getComponents($object, string $format = null, array $context)
240
    {
241
        $cacheKey = \get_class($object).'-'.$context['cache_key'];
242
243
        if (isset($this->componentsCache[$cacheKey])) {
244
            return $this->componentsCache[$cacheKey];
245
        }
246
247
        $attributes = parent::getAttributes($object, $format, $context);
248
249
        $options = $this->getFactoryOptions($context);
250
251
        $components = [
252
            'links' => [],
253
            'relationships' => [],
254
            'attributes' => [],
255
            'meta' => [],
256
        ];
257
258
        foreach ($attributes as $attribute) {
259
            $propertyMetadata = $this
260
                ->propertyMetadataFactory
261
                ->create($context['resource_class'], $attribute, $options);
262
263
            $type = $propertyMetadata->getType();
264
            $isOne = $isMany = false;
265
266
            if (null !== $type) {
267
                if ($type->isCollection()) {
268
                    $isMany = ($type->getCollectionValueType() && $className = $type->getCollectionValueType()->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false;
269
                } else {
270
                    $isOne = ($className = $type->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false;
271
                }
272
            }
273
274
            if (!isset($className) || !$isOne && !$isMany) {
275
                $components['attributes'][] = $attribute;
276
277
                continue;
278
            }
279
280
            $relation = [
281
                'name' => $attribute,
282
                'type' => $this->resourceMetadataFactory->create($className)->getShortName(),
283
                'cardinality' => $isOne ? 'one' : 'many',
284
            ];
285
286
            $components['relationships'][] = $relation;
287
        }
288
289
        if (false !== $context['cache_key']) {
290
            $this->componentsCache[$cacheKey] = $components;
291
        }
292
293
        return $components;
294
    }
295
296
    /**
297
     * Populates relationships keys.
298
     *
299
     * @param object      $object
300
     * @param string|null $format
301
     * @param array       $context
302
     * @param array       $relationships
303
     *
304
     * @throws InvalidArgumentException
305
     *
306
     * @return array
307
     */
308
    private function getPopulatedRelations($object, string $format = null, array $context, array $relationships): array
309
    {
310
        $data = [];
311
312
        if (!isset($context['resource_class'])) {
313
            return $data;
314
        }
315
316
        unset($context['api_included']);
317
        foreach ($relationships as $relationshipDataArray) {
318
            $relationshipName = $relationshipDataArray['name'];
319
320
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
321
322
            if ($this->nameConverter) {
323
                $relationshipName = $this->nameConverter->normalize($relationshipName);
324
            }
325
326
            if (!$attributeValue) {
327
                continue;
328
            }
329
330
            $data[$relationshipName] = [
331
                'data' => [],
332
            ];
333
334
            // Many to one relationship
335
            if ('one' === $relationshipDataArray['cardinality']) {
336
                unset($attributeValue['data']['attributes']);
337
                $data[$relationshipName] = $attributeValue;
338
339
                continue;
340
            }
341
342
            // Many to many relationship
343
            foreach ($attributeValue as $attributeValueElement) {
344
                if (!isset($attributeValueElement['data'])) {
345
                    throw new InvalidArgumentException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
346
                }
347
                unset($attributeValueElement['data']['attributes']);
348
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
349
            }
350
        }
351
352
        return $data;
353
    }
354
355
    /**
356
     * Populates included keys.
357
     */
358
    private function getRelatedResources($object, string $format = null, array $context, array $relationships): array
359
    {
360
        if (!isset($context['api_included'])) {
361
            return [];
362
        }
363
364
        $included = [];
365
        foreach ($relationships as $relationshipDataArray) {
366
            if (!\in_array($relationshipDataArray['name'], $context['api_included'], true)) {
367
                continue;
368
            }
369
370
            $relationshipName = $relationshipDataArray['name'];
371
            $relationContext = $context;
372
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
373
374
            if (!$attributeValue) {
375
                continue;
376
            }
377
378
            // Many to one relationship
379
            if ('one' === $relationshipDataArray['cardinality']) {
380
                $included[] = $attributeValue['data'];
381
382
                continue;
383
            }
384
            // Many to many relationship
385
            foreach ($attributeValue as $attributeValueElement) {
386
                if (isset($attributeValueElement['data'])) {
387
                    $included[] = $attributeValueElement['data'];
388
                }
389
            }
390
        }
391
392
        return $included;
393
    }
394
395
    /**
396
     * Gets the cache key to use.
397
     *
398
     * @param string|null $format
399
     * @param array       $context
400
     *
401
     * @return bool|string
402
     */
403
    private function getJsonApiCacheKey(string $format = null, array $context)
404
    {
405
        try {
406
            return md5($format.serialize($context));
407
        } catch (\Exception $exception) {
408
            // The context cannot be serialized, skip the cache
409
            return false;
410
        }
411
    }
412
}
413