Completed
Pull Request — master (#7)
by Yonel Ceruto
10:22
created

AuthorizationCheckerWrapper::isGranted()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 6
nop 2
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\Security\Core\Authorization\AuthorizationCheckerInterface;
14
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
15
16
class AuthorizationCheckerWrapper implements AuthorizationCheckerInterface
17
{
18
    private static $cache = [];
19
20
    private $authorizationChecker;
21
22
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
23
    {
24
        $this->authorizationChecker = $authorizationChecker;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function isGranted($attributes, $subject = null): bool
31
    {
32
        if (null !== $subject) {
33
            $key = md5(json_encode($attributes).'/'.spl_object_hash($subject));
34
        } else {
35
            $key = md5(json_encode($attributes));
36
        }
37
38
        if (array_key_exists($key, static::$cache)) {
0 ignored issues
show
Bug introduced by
Since $cache is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $cache to at least protected.
Loading history...
39
            return static::$cache[$key];
40
        }
41
42
        try {
43
            $isGranted = $this->authorizationChecker->isGranted($attributes, $subject);
44
        } catch (AuthenticationCredentialsNotFoundException $e) {
45
            $isGranted = false;
46
        }
47
48
        return static::$cache[$key] = $isGranted;
49
    }
50
}
51