Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

ComponentContextBuilder::createFromRequest()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 18
nc 17
nop 3
dl 0
loc 28
ccs 0
cts 19
cp 0
crap 56
rs 8.8333
c 1
b 1
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Serializer\ContextBuilder;
15
16
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
17
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
18
use Silverback\ApiComponentsBundle\Serializer\MappingLoader\ComponentLoader;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
21
use Symfony\Component\Security\Core\Security;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
final class ComponentContextBuilder implements SerializerContextBuilderInterface
27
{
28
    private SerializerContextBuilderInterface $decorated;
29
    private RoleHierarchyInterface $roleHierarchy;
30
    private Security $security;
31
32
    public function __construct(SerializerContextBuilderInterface $decorated, RoleHierarchyInterface $roleHierarchy, Security $security)
33
    {
34
        $this->decorated = $decorated;
35
        $this->roleHierarchy = $roleHierarchy;
36
        $this->security = $security;
37
    }
38
39
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
40
    {
41
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
42
        if (!is_a($resourceClass = $context['resource_class'], AbstractComponent::class, true)) {
43
            return $context;
44
        }
45
46
        $reflectionClass = new \ReflectionClass($resourceClass);
47
        $shortName = $reflectionClass->getShortName();
48
        $componentNames = [$shortName];
49
        while ($parent = $reflectionClass->getParentClass()) {
50
            $componentNames[] = $parent->getShortName();
51
            $reflectionClass = $parent;
52
        }
53
        $rw = $normalization ? 'read' : 'write';
54
        foreach ($componentNames as $componentName) {
55
            $context['groups'][] = sprintf('%s:%s:%s', $componentName, ComponentLoader::GROUP_NAME, $rw);
56
        }
57
58
        $user = $this->security->getUser();
59
        if ($user) {
60
            $reachableRoles = $this->roleHierarchy->getReachableRoleNames($user->getRoles());
61
            foreach ($reachableRoles as $reachableRole) {
62
                $context['groups'][] = sprintf('%s:%s:%s:%s', $shortName, ComponentLoader::GROUP_NAME, $rw, strtolower($reachableRole));
63
            }
64
        }
65
66
        return $context;
67
    }
68
}
69