Passed
Push — feature/unit-tests ( f5904a...516cc7 )
by Daniel
05:29 queued 37s
created

UserContextBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromRequest() 0 20 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;
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