Passed
Push — v2 ( 9fd9c6...30841d )
by Daniel
04:45 queued 10s
created

classHasSerializerGroupsOnProperties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentBundle\Serializer;
15
16
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
20
21
class AdminContextBuilder implements SerializerContextBuilderInterface
22
{
23
    private SerializerContextBuilderInterface $decorated;
24
    private AuthorizationCheckerInterface $authorizationChecker;
25
    private ClassMetadataFactoryInterface $classMetadataFactory;
26
27
    public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker, ClassMetadataFactoryInterface $classMetadataFactory)
28
    {
29
        $this->decorated = $decorated;
30
        $this->authorizationChecker = $authorizationChecker;
31
        $this->classMetadataFactory = $classMetadataFactory;
32
    }
33
34
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
35
    {
36
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
37
38
        $serializerGroupsConfigured = isset($context['groups']) && \is_array($context['groups']);
39
        if (!$serializerGroupsConfigured && $this->classHasSerializerGroupsOnProperties($context['resource_class'])) {
40
            $context['groups'] = [];
41
            $serializerGroupsConfigured = true;
42
        }
43
44
        if ($serializerGroupsConfigured) {
45
            $context['groups'] = $this->getAllSerializationGroups($context['groups'], $normalization);
46
        }
47
48
        return $context;
49
    }
50
51
    private function classHasSerializerGroupsOnProperties(string $resourceClass): bool
52
    {
53
        $serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($resourceClass);
54
        $serializerAttributeMetadata = $serializerClassMetadata->getAttributesMetadata();
55
        foreach ($serializerAttributeMetadata as $metadata) {
56
            if (\count($metadata->groups)) {
57
                return true;
58
            }
59
        }
60
61
        return false;
62
    }
63
64
    private function getAllSerializationGroups(array $groups, bool $normalization): array
65
    {
66
        array_push($groups, ...$this->getSerializationGroups('default', $normalization));
67
        if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
68
            array_push($groups, ...$this->getSerializationGroups('admin', $normalization));
69
        }
70
        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
71
            array_push($groups, ...$this->getSerializationGroups('super_admin', $normalization));
72
        }
73
74
        return $groups;
75
    }
76
77
    private function getSerializationGroups(string $groupName, bool $normalization): array
78
    {
79
        return [$groupName, sprintf('%s_%s', $groupName, $normalization ? 'read' : 'write')];
80
    }
81
}
82