Completed
Pull Request — 2.0 (#1141)
by Amrouche
03:37
created

SerializerContextBuilder::createFromRequest()   C

Complexity

Conditions 14
Paths 193

Size

Total Lines 57
Code Lines 35

Duplication

Lines 3
Ratio 5.26 %

Importance

Changes 0
Metric Value
dl 3
loc 57
rs 5.931
c 0
b 0
f 0
cc 14
eloc 35
nc 193
nop 3

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\Util\RequestAttributesExtractor;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * {@inheritdoc}
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
final class SerializerContextBuilder implements SerializerContextBuilderInterface
28
{
29
    private $resourceMetadataFactory;
30
31
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
32
    {
33
        $this->resourceMetadataFactory = $resourceMetadataFactory;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
40
    {
41 View Code Duplication
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            throw new RuntimeException('Request attributes are not valid.');
43
        }
44
45
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
46
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
47
48
        $operationKey = null;
49
        $operationType = null;
50
51
        if (isset($attributes['collection_operation_name'])) {
52
            $operationKey = 'collection_operation_name';
53
            $operationType = OperationType::COLLECTION;
54
        } elseif (isset($attributes['subresource_operation_name'])) {
55
            $operationKey = 'subresource_operation_name';
56
            $operationType = OperationType::SUBRESOURCE;
57
        }
58
59
        if (null !== $operationKey) {
60
            $attribute = $attributes[$operationKey];
61
            $context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true);
62
            $context[$operationKey] = $attribute;
63
        } else {
64
            $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true);
65
            $context['item_operation_name'] = $attributes['item_operation_name'];
66
        }
67
68
        $context['operation_type'] = $operationType ? $operationType : OperationType::ITEM;
69
70
        if (!$normalization && !isset($context['api_allow_update'])) {
71
            $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod();
72
        }
73
74
        $context['resource_class'] = $attributes['resource_class'];
75
        $context['request_uri'] = $request->getRequestUri();
76
77
        if (isset($attributes['subresource_context'])) {
78
            $context['subresource_identifiers'] = [];
79
80
            foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, $resourceClass)) {
81
                if (!isset($context['subresource_resources'][$resourceClass])) {
82
                    $context['subresource_resources'][$resourceClass] = [];
83
                }
84
85
                $context['subresource_identifiers'][$id] = $context['subresource_resources'][$resourceClass][$id] = $request->attributes->get($id);
86
            }
87
        }
88
89
        if (isset($attributes['subresource_property'])) {
90
            $context['subresource_property'] = $attributes['subresource_property'];
91
            $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null;
92
        }
93
94
        return $context;
95
    }
96
}
97