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

SerializerContextBuilder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 4.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 3
loc 70
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C createFromRequest() 3 57 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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