Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

ApiContextBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
C createFromRequest() 0 43 12
A __construct() 0 3 1
A getGroups() 0 7 3
A matchClass() 0 3 2
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
    private function getGroups(string $group, bool $normalization, ?string $operation)
25
    {
26
        $groups = [$group, $group . ($normalization ? '_read' : '_write')];
27
        if ($operation) {
28
            $groups[] = "${group}_${operation}";
29
        }
30
        return $groups;
31
    }
32
33
    private function matchClass($className, $matchClassName)
34
    {
35
        return $className === $matchClassName || is_subclass_of($className, $matchClassName);
36
    }
37
38
    /**
39
     * @param Request $request
40
     * @param bool $normalization
41
     * @param array|null $extractedAttributes
42
     * @return array
43
     * @throws \ApiPlatform\Core\Exception\RuntimeException
44
     */
45
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array
46
    {
47
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
48
        if (isset($context['groups']) && in_array('none', $context['groups'])) {
49
            return $context;
50
        }
51
        $subject = $request->attributes->get('_api_resource_class');
52
        $operation = $context['item_operation_name'] ?? null;
53
        $groups = [];
54
        if (
55
            $this->matchClass($subject, AbstractComponent::class) ||
56
            $this->matchClass($subject, AbstractNavigation::class) ||
57
            $this->matchClass($subject, ComponentLocation::class)
58
        ) {
59
            $groups[] = $this->getGroups('component', $normalization, $operation);
60
        }
61
        if (
62
            $this->matchClass($subject, AbstractNavigationItem::class)
63
        ) {
64
            $groups[] = $this->getGroups('component_item', $normalization, $operation);
65
        }
66
        if (
67
            $this->matchClass($subject, AbstractContent::class)
68
        ) {
69
            $groups[] = $this->getGroups('content', $normalization, $operation);
70
        }
71
        if (
72
            $this->matchClass($subject, Route::class)
73
        ) {
74
            $groups[] = $this->getGroups('route', $normalization, $operation);
75
        }
76
        if ($this->matchClass($subject, Layout::class)) {
77
            $groups[] = $this->getGroups('layout', $normalization, $operation);
78
        }
79
        if (\count($groups)) {
80
            if (!isset($context['groups'])) {
81
                $context['groups'] = ['default'];
82
            } else {
83
                $context['groups'][] = ['default'];
84
            }
85
            $context['groups'] = array_merge($context['groups'], ...$groups);
0 ignored issues
show
Bug introduced by
$groups is expanded, but the parameter $array2 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

85
            $context['groups'] = array_merge($context['groups'], /** @scrutinizer ignore-type */ ...$groups);
Loading history...
86
        }
87
        return $context;
88
    }
89
}
90