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

SuperAdminContextBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 10

2 Methods

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