Passed
Push — v2 ( c6edc5...9b02b7 )
by Daniel
04:21
created

AdminContextBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 27
cp 0
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A classHasSerializerGroupsOnProperties() 0 10 3
A getSerializationGroups() 0 3 2
A __construct() 0 5 1
B createFromRequest() 0 21 7
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
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
21
22
class AdminContextBuilder implements SerializerContextBuilderInterface
23
{
24
    private SerializerContextBuilderInterface $decorated;
25
    private AuthorizationCheckerInterface $authorizationChecker;
26
    private ClassMetadataFactoryInterface $classMetadataFactory;
27
28
    public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker, ClassMetadataFactoryInterface $classMetadataFactory)
29
    {
30
        $this->decorated = $decorated;
31
        $this->authorizationChecker = $authorizationChecker;
32
        $this->classMetadataFactory = $classMetadataFactory;
33
    }
34
35
    private function classHasSerializerGroupsOnProperties(string $resourceClass): bool
36
    {
37
        $serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($resourceClass);
38
        $serializerAttributeMetadata = $serializerClassMetadata->getAttributesMetadata();
39
        foreach ($serializerAttributeMetadata as $metadata) {
40
            if (count($metadata->groups)) {
41
                return true;
42
            }
43
        }
44
        return false;
45
    }
46
47
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
48
    {
49
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
50
51
        $serializerGroupsConfigured = isset($context['groups']) && is_array($context['groups']);
52
        if (!$serializerGroupsConfigured && $this->classHasSerializerGroupsOnProperties($context['resource_class'])) {
53
            $context['groups'] = [];
54
            $serializerGroupsConfigured = true;
55
        }
56
57
        if ($serializerGroupsConfigured) {
58
            array_push($context['groups'], ...$this->getSerializationGroups('default', $normalization));
59
            if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
60
                array_push($context['groups'], ...$this->getSerializationGroups('admin', $normalization));
61
            }
62
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
63
                array_push($context['groups'], ...$this->getSerializationGroups('super_admin', $normalization));
64
            }
65
        }
66
67
        return $context;
68
    }
69
70
    private function getSerializationGroups(string $groupName, bool $normalization): array
71
    {
72
        return [$groupName, sprintf('%s_%s', $groupName, $normalization ? 'read' : 'write')];
73
    }
74
}
75