Passed
Push — feature/publishable ( c26268...7bd202 )
by Daniel
18:12 queued 11:31
created

UserContextBuilder::createFromRequest()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.2222
cc 6
nc 17
nop 3
crap 6
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\ContextBuilder;
15
16
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
17
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20
21
class UserContextBuilder implements SerializerContextBuilderInterface
22
{
23
    private SerializerContextBuilderInterface $decorated;
24
    private AuthorizationCheckerInterface $authorizationChecker;
25
26 5
    public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker)
27
    {
28 5
        $this->decorated = $decorated;
29 5
        $this->authorizationChecker = $authorizationChecker;
30 5
    }
31
32 5
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
33
    {
34 5
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
35 5
        $resourceClass = @$context['resource_class'] ?? null;
36 5
        if (!is_subclass_of($resourceClass, AbstractUser::class)) {
37 2
            return $context;
38
        }
39
40 3
        $serializerGroupsConfigured = isset($context['groups']) && \is_array($context['groups']);
41 3
        if (!$serializerGroupsConfigured) {
42 2
            $context['groups'] = [];
43
        }
44 3
        $postfix = false === $normalization ? 'input' : 'output';
45 3
        $context['groups'][] = sprintf('User:%s', $postfix);
46
47 3
        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
48 1
            $context['groups'][] = 'User:super_admin';
49
        }
50
51 3
        return $context;
52
    }
53
}
54