Completed
Push — master ( 183b0a...e3c17c )
by Kévin
03:20
created

SerializerFilterContextBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 6.12 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 3
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D createFromRequest() 3 32 9

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\Exception\RuntimeException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\Serializer\Filter\FilterInterface;
19
use ApiPlatform\Core\Util\RequestAttributesExtractor;
20
use Psr\Container\ContainerInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * {@inheritdoc}
25
 *
26
 * @author Baptiste Meyer <[email protected]>
27
 */
28
final class SerializerFilterContextBuilder implements SerializerContextBuilderInterface
29
{
30
    private $decorated;
31
    private $filterLocator;
32
    private $resourceMetadataFactory;
33
34
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ContainerInterface $filterLocator, SerializerContextBuilderInterface $decorated)
35
    {
36
        $this->decorated = $decorated;
37
        $this->filterLocator = $filterLocator;
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
45
    {
46 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...
47
            throw new RuntimeException('Request attributes are not valid.');
48
        }
49
50
        $context = $this->decorated->createFromRequest($request, $normalization, $attributes);
51
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
52
53
        if (isset($attributes['collection_operation_name']) || isset($attributes['subresource_operation_name'])) {
54
            $resourceFilters = $resourceMetadata->getCollectionOperationAttribute(
55
                $attributes['collection_operation_name'] ?? $attributes['subresource_operation_name'],
56
                'filters',
57
                [],
58
                true
59
            );
60
        } else {
61
            $resourceFilters = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'filters', [], true);
62
        }
63
64
        if (!$resourceFilters) {
65
            return $context;
66
        }
67
68
        foreach ($resourceFilters as $filterId) {
69
            if ($this->filterLocator->has($filterId) && ($filter = $this->filterLocator->get($filterId)) instanceof FilterInterface) {
70
                $filter->apply($request, $normalization, $attributes, $context);
71
            }
72
        }
73
74
        return $context;
75
    }
76
}
77