EndpointResolver::resolveEndpoint()   C
last analyzed

Complexity

Conditions 12
Paths 56

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12.0104

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
c 1
b 0
f 1
dl 0
loc 45
ccs 23
cts 24
cp 0.9583
rs 6.9666
cc 12
nc 56
nop 1
crap 12.0104

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
use Ynlo\GraphQLBundle\Extension\EndpointNotValidException;
20
21
class EndpointResolver
22
{
23
    /**
24
     * @var DefinitionRegistry
25
     */
26
    protected $definitionRegistry;
27
28
    /**
29
     * @var AuthorizationChecker
30
     */
31
    protected $authorizationChecker;
32
33
    /**
34
     * @var array
35
     */
36
    protected $endpointsConfig = [];
37
38
    /**
39
     * EndpointResolver constructor.
40
     *
41
     * Endpoints config should have the following format
42
     *
43
     * [
44
     * 'endpoints' => [
45
     *   'name' => [
46
     *      'roles'=> [],
47
     *      'host' => '',
48
     *      'path' => ''
49
     *    ]
50
     *  ]
51
     * ]
52
     *
53
     * @param DefinitionRegistry            $definitionRegistry
54
     * @param AuthorizationCheckerInterface $authorizationChecker
55
     * @param array                         $endpointsConfig
56
     */
57 5
    public function __construct(DefinitionRegistry $definitionRegistry, AuthorizationCheckerInterface $authorizationChecker, array $endpointsConfig = [])
58
    {
59 5
        $this->definitionRegistry = $definitionRegistry;
60 5
        $this->authorizationChecker = $authorizationChecker;
0 ignored issues
show
Documentation Bug introduced by
$authorizationChecker is of type Symfony\Component\Securi...izationCheckerInterface, but the property $authorizationChecker was declared to be of type Symfony\Component\Securi...on\AuthorizationChecker. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
61 5
        $this->endpointsConfig = $endpointsConfig['endpoints'] ?? [];
62 5
    }
63
64
    /**
65
     * @param Request $request
66
     *
67
     * @return null|Endpoint
68
     *
69
     * @throws EndpointNotValidException
70
     */
71 5
    public function resolveEndpoint(Request $request): ?Endpoint
72
    {
73 5
        if (empty($this->endpointsConfig)) {
74 1
            return $this->definitionRegistry->getEndpoint();
75
        }
76
77 4
        foreach ($this->endpointsConfig as $endpoint => $config) {
78 4
            if (isset($config['host'])) {
79 2
                $host = $request->getHost();
80 2
                if (preg_match(sprintf('/%s/', $this->cleanExpression($config['host'])), $host)) {
0 ignored issues
show
Bug introduced by
It seems like $this->cleanExpression($config['host']) can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                if (preg_match(sprintf('/%s/', /** @scrutinizer ignore-type */ $this->cleanExpression($config['host'])), $host)) {
Loading history...
81 1
                    $hostPassed = true;
82
                } else {
83 2
                    $hostPassed = false;
84
                }
85
            } else {
86 2
                $hostPassed = true;
87
            }
88
89 4
            if (isset($config['path'])) {
90 1
                $path = $request->getPathInfo();
91 1
                if (preg_match(sprintf('/%s/', $this->cleanExpression($config['path'])), $path)) {
92 1
                    $pathPassed = true;
93
                } else {
94 1
                    $pathPassed = false;
95
                }
96
            } else {
97 3
                $pathPassed = true;
98
            }
99
100 4
            if (isset($config['roles'])) {
101
                try {
102 1
                    $rolePassed = $this->authorizationChecker->isGranted($config['roles']);
103
                } catch (AuthenticationCredentialsNotFoundException $exception) {
104 1
                    $rolePassed = false;
105
                }
106
            } else {
107 3
                $rolePassed = true;
108
            }
109
110 4
            if ($rolePassed && $hostPassed && $pathPassed) {
111 3
                return $this->definitionRegistry->getEndpoint($endpoint);
112
            }
113
        }
114
115 1
        return null;
116
    }
117
118
    /**
119
     * @param string $exp
120
     *
121
     * @return null|string|string[]
122
     */
123 3
    private function cleanExpression($exp)
124
    {
125 3
        return preg_replace('/\//', '\/', $exp);
126
    }
127
}
128