Completed
Pull Request — master (#7)
by Yonel Ceruto
10:45 queued 04:14
created

RolesDefinitionExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 48
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A secureDefinitions() 0 14 4
A configureEndpoint() 0 3 1
A buildConfig() 0 6 1
1
<?php
2
3
/*******************************************************************************
4
 *  This file is part of the GraphQL Bundle package.
5
 *
6
 *  (c) YnloUltratech <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 ******************************************************************************/
11
12
namespace Ynlo\GraphQLBundle\Definition\Extension;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
18
19
class RolesDefinitionExtension extends AbstractDefinitionExtension
20
{
21
    private $authorizationChecker;
22
23 22
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
24
    {
25 22
        $this->authorizationChecker = $authorizationChecker;
26 22
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 22
    public function buildConfig(ArrayNodeDefinition $root): void
32
    {
33
        $root
34 22
            ->info('List of roles for queries and mutations')
35 22
            ->prototype('scalar')
36 22
            ->end();
37 22
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 22
    public function configureEndpoint(Endpoint $endpoint): void
43
    {
44 22
        $endpoint->setQueries($this->secureDefinitions($endpoint->allQueries(), $endpoint));
45 22
    }
46
47
    /**
48
     * @param ExecutableDefinitionInterface[] $definitions
49
     * @param Endpoint                        $endpoint
50
     *
51
     * @return ExecutableDefinitionInterface[]
52
     */
53 22
    private function secureDefinitions(array $definitions, Endpoint $endpoint): array
0 ignored issues
show
Unused Code introduced by
The parameter $endpoint is not used and could be removed. ( Ignorable by Annotation )

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

53
    private function secureDefinitions(array $definitions, /** @scrutinizer ignore-unused */ Endpoint $endpoint): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55 22
        $secureDefinitions = [];
56 22
        foreach ($definitions as $definition) {
57 22
            if ($roles = $definition->getRoles()) {
0 ignored issues
show
Bug introduced by
The method getRoles() does not exist on Ynlo\GraphQLBundle\Defin...ableDefinitionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Ynlo\GraphQLBundle\Defin...ableDefinitionInterface. ( Ignorable by Annotation )

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

57
            if ($roles = $definition->/** @scrutinizer ignore-call */ getRoles()) {
Loading history...
58
                if ($this->authorizationChecker->isGranted($roles)) {
59
                    $secureDefinitions[] = $definition;
60
                }
61
            } else {
62 22
                $secureDefinitions[] = $definition;
63
            }
64
        }
65
66 22
        return $secureDefinitions;
67
    }
68
}
69