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

AuthorizationCheckerWrapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

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