Passed
Push — develop ( 65a498...5c9501 )
by Daniel
06:03
created

ApiContextBuilder::createFromRequest()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 3
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Serializer;
4
5
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
6
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
7
use Silverback\ApiComponentBundle\Entity\Content\Component\AbstractComponent;
8
use Silverback\ApiComponentBundle\Entity\Content\Component\ComponentLocation;
9
use Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\AbstractNavigation;
10
use Silverback\ApiComponentBundle\Entity\Content\Component\Navigation\AbstractNavigationItem;
11
use Silverback\ApiComponentBundle\Entity\Layout\Layout;
12
use Silverback\ApiComponentBundle\Entity\Route\Route;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class ApiContextBuilder implements SerializerContextBuilderInterface
16
{
17
    /**
18
     * @var string[][]
19
     */
20
    public const CLASS_GROUP_MAPPING = [
21
        AbstractComponent::class => ['component'],
22
        AbstractNavigation::class => ['component'],
23
        ComponentLocation::class => ['component'],
24
        AbstractContent::class => ['content'],
25
        Route::class => ['route'],
26
        Layout::class => ['layout']
27
    ];
28
29
    /**
30
     * @var SerializerContextBuilderInterface
31
     */
32
    private $decorated;
33
34 8
    public function __construct(SerializerContextBuilderInterface $decorated)
35
    {
36 8
        $this->decorated = $decorated;
37 8
    }
38
39
    /**
40
     * @param string $group
41
     * @param bool $normalization
42
     * @return array
43
     */
44 7
    private function getGroupNames(string $group, bool $normalization): array
45
    {
46 7
        return [$group, $group . ($normalization ? '_read' : '_write')];
47
    }
48
49
    /**
50
     * @param $className
51
     * @param $matchClassName
52
     * @return bool
53
     */
54 7
    private function matchClass($className, $matchClassName): bool
55
    {
56 7
        return $className === $matchClassName || is_subclass_of($className, $matchClassName);
57
    }
58
59
    /**
60
     * @param string $subject
61
     * @param bool $normalization
62
     * @return array
63
     */
64 7
    private function getGroups(string $subject, bool $normalization): array
65
    {
66
        /** @var string[] $groups */
67
        $groups = [[
68 7
            'default',
69 7
            'default' . ($normalization ? '_read' : '_write')
70
        ]];
71 7
        foreach (self::CLASS_GROUP_MAPPING as $class=>$groupMapping) {
72 7
            if ($this->matchClass($subject, $class)) {
73 7
                foreach ($groupMapping as $group) {
74 7
                    $groups[] = $this->getGroupNames($group, $normalization);
75
                }
76
            }
77
        }
78 7
        return $groups;
79
    }
80
81
    /**
82
     * @param Request $request
83
     * @param bool $normalization
84
     * @param array|null $extractedAttributes
85
     * @return array
86
     * @throws \ApiPlatform\Core\Exception\RuntimeException
87
     */
88 8
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null) : array
89
    {
90 8
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
91 8
        if (isset($context['groups']) && \in_array('none', $context['groups'], true)) {
92 5
            return $context;
93
        }
94 7
        $subject = $request->attributes->get('_api_resource_class');
95 7
        $groups = $this->getGroups($subject, $normalization);
96 7
        if (\count($groups)) {
97 7
            $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

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