Passed
Push — v2 ( dec916...f92e78 )
by Daniel
05:10
created

SuperAdminContextBuilder::createFromRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
cc 4
nc 4
nop 3
crap 20
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
20
class SuperAdminContextBuilder implements SerializerContextBuilderInterface
21
{
22
    private SerializerContextBuilderInterface $decorated;
23
    private AuthorizationCheckerInterface $authorizationChecker;
24
25
    public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker)
26
    {
27
        $this->decorated = $decorated;
28
        $this->authorizationChecker = $authorizationChecker;
29
    }
30
31
    public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
32
    {
33
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
34
        if (!isset($context['groups'])) {
35
            $context['groups'] = ['default'];
36
        }
37
        if (isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
38
            $context['groups'][] = 'super_admin';
39
        }
40
41
        return $context;
42
    }
43
}
44