Completed
Pull Request — master (#904)
by Antoine
04:04
created

SerializerContextBuilder::createFromRequest()   D

Complexity

Conditions 10
Paths 25

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 4.8196
cc 10
eloc 21
nc 25
nop 3

How to fix   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
namespace ApiPlatform\Core\Serializer;
13
14
use ApiPlatform\Core\Exception\RuntimeException;
15
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
16
use ApiPlatform\Core\Util\RequestAttributesExtractor;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * {@inheritdoc}
21
 *
22
 * @author Kévin Dunglas <[email protected]>
23
 */
24
final class SerializerContextBuilder implements SerializerContextBuilderInterface
25
{
26
    private $resourceMetadataFactory;
27
28
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
29
    {
30
        $this->resourceMetadataFactory = $resourceMetadataFactory;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
37
    {
38
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
39
            throw new RuntimeException('Request attributes are not valid.');
40
        }
41
42
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
43
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
44
45
        if (isset($attributes['collection_operation_name']) || isset($attributes['subresource_operation_name'])) {
46
            $operationKey = isset($attributes['collection_operation_name']) ? 'collection_operation_name' : 'subresource_operation_name';
47
            $attribute = $attributes[$operationKey];
48
            $context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true);
49
            $context[$operationKey] = $attribute;
50
        } else {
51
            $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true);
52
            $context['item_operation_name'] = $attributes['item_operation_name'];
53
        }
54
55
        if (!$normalization && !isset($context['api_allow_update'])) {
56
            $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod();
57
        }
58
59
        $context['resource_class'] = $attributes['resource_class'];
60
        $context['request_uri'] = $request->getRequestUri();
61
62
        if (isset($attributes['subresource_property'])) {
63
            $context['subresource_property'] = $attributes['subresource_property'];
64
            $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null;
65
        }
66
67
        return $context;
68
    }
69
}
70