Passed
Push — v2 ( 9b853e...868892 )
by Daniel
04:58
created

AdminContextBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 AdminContextBuilder 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'])) {
38
            if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
39
                $context['groups'][] = 'admin';
40
            }
41
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
42
                $context['groups'][] = 'super_admin';
43
            }
44
        }
45
46
        return $context;
47
    }
48
}
49