Passed
Push — develop ( 32fbc0...1b1d1e )
by Daniel
05:39
created

ApiContextBuilder::getGroupNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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

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