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

EndpointResolver::resolveEndpoint()   C

Complexity

Conditions 12
Paths 56

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
ccs 12
cts 24
cp 0.5
rs 5.1612
cc 12
eloc 29
nc 56
nop 1
crap 30

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\Exception\AuthenticationCredentialsNotFoundException;
16
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
17
18
class EndpointResolver
19
{
20
    /**
21
     * @var AuthorizationChecker
22
     */
23
    protected $authorizationChecker;
24
25
    /**
26
     * @var array
27
     */
28
    protected $endpointsConfig = [];
29
30 22
    public function __construct(AuthorizationChecker $authorizationChecker, array $endpointsConfig = [])
31
    {
32 22
        $this->authorizationChecker = $authorizationChecker;
33 22
        $this->endpointsConfig = $endpointsConfig['endpoints'] ?? [];
34 22
    }
35
36 22
    public function resolveEndpoint(Request $request): ?string
37
    {
38 22
        if (empty($this->endpointsConfig)) {
39
            return DefinitionRegistry::DEFAULT_ENDPOINT;
40
        }
41
42 22
        foreach ($this->endpointsConfig as $endpoint => $config) {
43 22
            if (isset($config['host'])) {
44
                $host = $request->getHost();
45
                if (preg_match(sprintf('/%s/', $this->cleanExpression($config['host'])), $host)) {
46
                    $hostPassed = true;
47
                } else {
48
                    $hostPassed = false;
49
                }
50
            } else {
51 22
                $hostPassed = true;
52
            }
53
54 22
            if (isset($config['path'])) {
55
                $path = $request->getPathInfo();
56
                if (preg_match(sprintf('/%s/', $this->cleanExpression($config['path'])), $path)) {
57
                    $pathPassed = true;
58
                } else {
59
                    $pathPassed = false;
60
                }
61
            } else {
62 22
                $pathPassed = true;
63
            }
64
65 22
            if (isset($config['roles'])) {
66
                try {
67 22
                    $rolePassed = $this->authorizationChecker->isGranted($config['roles']);
68
                } catch (AuthenticationCredentialsNotFoundException $exception) {
69 22
                    $rolePassed = false;
70
                }
71
            } else {
72
                $rolePassed = true;
73
            }
74
75 22
            if ($rolePassed && $hostPassed && $pathPassed) {
76 22
                return $endpoint;
77
            }
78
        }
79
80
        return null;
81
    }
82
83
    private function cleanExpression($exp)
84
    {
85
        return preg_replace('/\//', '\/', $exp);
86
    }
87
}
88