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

SerializerContextBuilder::createFromRequest()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 48
nop 3
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\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
15
use ApiPlatform\Core\Util\RequestAttributesExtractor;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * {@inheritdoc}
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 */
23
final class SerializerContextBuilder implements SerializerContextBuilderInterface
24
{
25
    private $resourceMetadataFactory;
26
27
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
28
    {
29
        $this->resourceMetadataFactory = $resourceMetadataFactory;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
36
    {
37
        if (null === $attributes) {
38
            $attributes = RequestAttributesExtractor::extractAttributes($request);
39
        }
40
41
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
42
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
43
44
        if (isset($attributes['collection_operation_name']) || isset($attributes['subcollection_operation_name'])) {
45
            $operationKey = isset($attributes['collection_operation_name']) ? 'collection_operation_name' : 'subcollection_operation_name';
46
            $attribute = $attributes[$operationKey];
47
            $context = $resourceMetadata->getCollectionOperationAttribute($attribute, $key, [], true);
48
            $context[$operationKey] = $attribute;
49
        } else {
50
            $context = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], $key, [], true);
51
            $context['item_operation_name'] = $attributes['item_operation_name'];
52
        }
53
54
        if (!$normalization && !isset($context['api_allow_update'])) {
55
            $context['api_allow_update'] = Request::METHOD_PUT === $request->getMethod();
56
        }
57
58
        $context['resource_class'] = $attributes['resource_class'];
59
        $context['request_uri'] = $request->getRequestUri();
60
61
        if (isset($attributes['subcollection_property'])) {
62
            $context['subcollection_property'] = $attributes['subcollection_property'];
63
            $context['subcollection_resource_class'] = $attributes['subcollection_resource_class'] ?? null;
64
        }
65
66
        return $context;
67
    }
68
}
69