Completed
Push — master ( 8253ba...a023d2 )
by Daniel
16:49 queued 05:23
created

ApiContextBuilder::createFromRequest()   C

Complexity

Conditions 12
Paths 97

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 30
nc 97
nop 3
dl 0
loc 44
ccs 0
cts 25
cp 0
crap 156
rs 5.1612
c 0
b 0
f 0

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
namespace Silverback\ApiComponentBundle\Serializer;
4
5
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
6
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent;
7
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
8
use Silverback\ApiComponentBundle\Entity\Content\ComponentLocation;
9
use Silverback\ApiComponentBundle\Entity\Layout\Layout;
10
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigation;
11
use Silverback\ApiComponentBundle\Entity\Navigation\AbstractNavigationItem;
12
use Silverback\ApiComponentBundle\Entity\Navigation\Route\Route;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class ApiContextBuilder implements SerializerContextBuilderInterface
16
{
17
    private $decorated;
18
19
    public function __construct(SerializerContextBuilderInterface $decorated)
20
    {
21
        $this->decorated = $decorated;
22
    }
23
24
    /**
25
     * @param string $group
26
     * @param bool $normalization
27
     * @param null|string $operation
28
     * @return array
29
     */
30
    private function getGroups(string $group, bool $normalization, ?string $operation): array
31
    {
32
        $groups = [$group, $group . ($normalization ? '_read' : '_write')];
33
        if ($operation) {
34
            $groups[] = "${group}_${operation}";
35
        }
36
        return $groups;
37
    }
38
39
    /**
40
     * @param $className
41
     * @param $matchClassName
42
     * @return bool
43
     */
44
    private function matchClass($className, $matchClassName): bool
45
    {
46
        return $className === $matchClassName || is_subclass_of($className, $matchClassName);
47
    }
48
49
    /**
50
     * @param Request $request
51
     * @param bool $normalization
52
     * @param array|null $extractedAttributes
53
     * @return array
54
     * @throws \ApiPlatform\Core\Exception\RuntimeException
55
     */
56
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array
57
    {
58
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
59
        if (isset($context['groups']) && \in_array('none', $context['groups'], true)) {
60
            return $context;
61
        }
62
        $subject = $request->attributes->get('_api_resource_class');
63
        $operation = $context['item_operation_name'] ?? null;
64
        $groups = [];
65
        if (
66
            $this->matchClass($subject, AbstractComponent::class) ||
67
            $this->matchClass($subject, AbstractNavigation::class) ||
68
            $this->matchClass($subject, ComponentLocation::class)
69
        ) {
70
            $groups[] = $this->getGroups('component', $normalization, $operation);
71
        }
72
        if (
73
            $this->matchClass($subject, AbstractNavigationItem::class)
74
        ) {
75
            $groups[] = $this->getGroups('component_item', $normalization, $operation);
76
        }
77
        if (
78
            $this->matchClass($subject, AbstractContent::class)
79
        ) {
80
            $groups[] = $this->getGroups('content', $normalization, $operation);
81
        }
82
        if (
83
            $this->matchClass($subject, Route::class)
84
        ) {
85
            $groups[] = $this->getGroups('route', $normalization, $operation);
86
        }
87
        if ($this->matchClass($subject, Layout::class)) {
88
            $groups[] = $this->getGroups('layout', $normalization, $operation);
89
        }
90
        if (\count($groups)) {
91
            if (!isset($context['groups'])) {
92
                $context['groups'] = ['default'];
93
            } else {
94
                $context['groups'][] = ['default'];
95
            }
96
            array_unshift($groups, $context['groups']);
97
            $context['groups'] = array_merge( /** @scrutinizer ignore-type */ ...$groups);
0 ignored issues
show
Bug introduced by
$groups is expanded, but the parameter $array1 of array_merge() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            $context['groups'] = array_merge( /** @scrutinizer ignore-type */ /** @scrutinizer ignore-type */ ...$groups);
Loading history...
98
        }
99
        return $context;
100
    }
101
}
102