Passed
Pull Request — master (#3367)
by Alan
04:04
created

PropertyFactoryOptionsTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyFactoryOptions() 0 21 4
B getSerializationContext() 0 36 9
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\Serializer;
15
16
use ApiPlatform\Core\Api\OperationType;
17
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
20
21
/**
22
 * Manipulates the property factory options.
23
 *
24
 * @internal
25
 *
26
 * @author @author Alan Poulain <[email protected]>
27
 */
28
trait PropertyFactoryOptionsTrait
29
{
30
    /**
31
     * Gets the options for a property factory.
32
     */
33
    private function getPropertyFactoryOptions(string $resourceClass, bool $splitDeserialization = false): array
34
    {
35
        $serializationContext = $this->getSerializationContext($resourceClass, true);
36
        $deserializationContext = $this->getSerializationContext($resourceClass, false);
37
        $serializationGroups = (array) ($serializationContext[AbstractNormalizer::GROUPS] ?? []);
38
        $deserializationGroups = (array) ($deserializationContext[AbstractNormalizer::GROUPS] ?? []);
39
40
        if (!$splitDeserialization) {
41
            $serializationGroups = array_unique(array_merge($serializationGroups, $deserializationGroups));
42
            $deserializationGroups = [];
43
        }
44
45
        $options = [];
46
        if ($serializationGroups) {
47
            $options['serializer_groups'] = $serializationGroups;
48
        }
49
        if ($deserializationGroups) {
0 ignored issues
show
introduced by
$deserializationGroups is an empty array, thus is always false.
Loading history...
Bug Best Practice introduced by
The expression $deserializationGroups 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...
50
            $options['deserializer_groups'] = $deserializationGroups;
51
        }
52
53
        return $options;
54
    }
55
56
    /**
57
     * Get the serialization context using the serializer context builder if available or the resource attributes if not.
58
     */
59
    private function getSerializationContext(string $resourceClass, bool $normalization, ?string $operationType = null, ?string $operationName = null): array
60
    {
61
        try {
62
            if ($this->serializerContextBuilder) {
63
                switch ($operationType) {
64
                    case OperationType::ITEM:
65
                        $operationKey = 'item_operation_name';
66
                        break;
67
                    case OperationType::COLLECTION:
68
                        $operationKey = 'collection_operation_name';
69
                        break;
70
                    case OperationType::SUBRESOURCE:
71
                        $operationKey = 'subresource_operation_name';
72
                        break;
73
                    default:
74
                        $operationKey = 'resource_operation_name';
75
                }
76
77
                $request = Request::createFromGlobals();
78
79
                return $this->serializerContextBuilder->createFromRequest($request, $normalization, [
80
                    'resource_class' => $resourceClass,
81
                    $operationKey => $operationName ?? 'resource',
82
                ]);
83
            }
84
85
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
86
            $attribute = $normalization ? 'normalization_context' : 'denormalization_context';
87
88
            if (null === $operationType || null === $operationName) {
89
                return $resourceMetadata->getAttribute($attribute, []);
90
            }
91
92
            return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true);
93
        } catch (ResourceClassNotFoundException $exception) {
94
            return [];
95
        }
96
    }
97
}
98