Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

AccessControlChecker::isGranted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2.0625
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Security\Authorization;
12
13
use Symfony\Component\ExpressionLanguage\Expression;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
15
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
16
17
class AccessControlChecker
18
{
19
    /**
20
     * @var AuthorizationCheckerInterface
21
     */
22
    protected $authChecker;
23
24
    /**
25
     * AccessControlChecker constructor.
26
     *
27
     * @param AuthorizationCheckerInterface $authChecker
28
     */
29 22
    public function __construct(AuthorizationCheckerInterface $authChecker)
30
    {
31 22
        $this->authChecker = $authChecker;
32 22
    }
33
34
    /**
35
     * @param DefinitionInterface $definition
36
     * @param mixed|null          $subject
37
     *
38
     * @return bool
39
     */
40 3
    public function isGranted(DefinitionInterface $definition, $subject = null): bool
41
    {
42 3
        if ($this->isControlled($definition)) {
43 3
            return $this->authChecker->isGranted($this->getExpression($definition), $subject);
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * @param DefinitionInterface $definition
51
     *
52
     * @return null|string
53
     */
54
    public function getMessage(DefinitionInterface $definition): ?string
55
    {
56
        return $definition->getMeta('access_control')['message'] ?? null;
57
    }
58
59
    /**
60
     * @param DefinitionInterface $definition
61
     *
62
     * @return null|Expression
63
     */
64 3
    public function getExpression(DefinitionInterface $definition): ?Expression
65
    {
66 3
        $accessControl = $definition->getMeta('access_control', []);
67 3
        if ($expressionSerialized = $accessControl['expression_serialized'] ?? null) {
68 3
            $expression = unserialize($expressionSerialized, ['allowed_classes' => true]);
69
        } else {
70
            $rawExpression = $accessControl['expression'] ?? null;
71
            $expression = new Expression($rawExpression);
72
        }
73
74 3
        return $expression;
75
    }
76
77
    /**
78
     * @param DefinitionInterface $definition
79
     *
80
     * @return bool
81
     */
82 22
    public function isControlled(DefinitionInterface $definition): bool
83
    {
84 22
        return (bool) ($definition->getMeta('access_control', [])['expression'] ?? false);
85
    }
86
}
87