CwaResourceContextBuilder::createFromRequest()   B
last analyzed

Complexity

Conditions 8
Paths 17

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

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