Passed
Push — master ( 81d276...59fa1e )
by Kévin
04:12 queued 10s
created

SerializerContextBuilder::createFromRequest()   D

Complexity

Conditions 16
Paths 193

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 40
c 1
b 0
f 0
nc 193
nop 3
dl 0
loc 65
rs 4.7916

How to fix   Long Method    Complexity   

Long Method

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:

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\RuntimeException;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
20
use ApiPlatform\Core\Util\RequestAttributesExtractor;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * {@inheritdoc}
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class SerializerContextBuilder implements SerializerContextBuilderInterface
29
{
30
    private $resourceMetadataFactory;
31
32
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
33
    {
34
        $this->resourceMetadataFactory = $resourceMetadataFactory;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
41
    {
42
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
43
            throw new RuntimeException('Request attributes are not valid.');
44
        }
45
46
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
47
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
48
        if (isset($attributes['collection_operation_name'])) {
49
            $operationKey = 'collection_operation_name';
50
            $operationType = OperationType::COLLECTION;
51
        } elseif (isset($attributes['item_operation_name'])) {
52
            $operationKey = 'item_operation_name';
53
            $operationType = OperationType::ITEM;
54
        } else {
55
            $operationKey = 'subresource_operation_name';
56
            $operationType = OperationType::SUBRESOURCE;
57
        }
58
59
        $context = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], $key, [], true);
60
        $context['operation_type'] = $operationType;
61
        $context[$operationKey] = $attributes[$operationKey];
62
63
        if (!$normalization && !isset($context['api_allow_update'])) {
64
            $context['api_allow_update'] = \in_array($request->getMethod(), ['PUT', 'PATCH'], true);
65
        }
66
67
        $context['resource_class'] = $attributes['resource_class'];
68
        $context['input'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'input', null, true);
69
        $context['output'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'output', null, true);
70
        $context['request_uri'] = $request->getRequestUri();
71
        $context['uri'] = $request->getUri();
72
73
        if (isset($attributes['subresource_context'])) {
74
            $context['subresource_identifiers'] = [];
75
76
            foreach ($attributes['subresource_context']['identifiers'] as $key => [$id, $resourceClass]) {
77
                if (!isset($context['subresource_resources'][$resourceClass])) {
78
                    $context['subresource_resources'][$resourceClass] = [];
79
                }
80
81
                $context['subresource_identifiers'][$id] = $context['subresource_resources'][$resourceClass][$id] = $request->attributes->get($id);
82
            }
83
        }
84
85
        if (isset($attributes['subresource_property'])) {
86
            $context['subresource_property'] = $attributes['subresource_property'];
87
            $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null;
88
        }
89
90
        unset($context[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]);
91
92
        if (isset($context['skip_null_values'])) {
93
            return $context;
94
        }
95
96
        foreach ($resourceMetadata->getItemOperations() as $operation) {
97
            if ('PATCH' === $operation['method'] && \in_array('application/merge-patch+json', $operation['input_formats']['json'] ?? [], true)) {
98
                $context['skip_null_values'] = true;
99
100
                break;
101
            }
102
        }
103
104
        return $context;
105
    }
106
}
107