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

RolesDefinitionExtension::configureEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 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\DefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
20
class RolesDefinitionExtension extends AbstractDefinitionExtension
21
{
22
    private $authorizationChecker;
23
24
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
25
    {
26
        $this->authorizationChecker = $authorizationChecker;
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function buildConfig(ArrayNodeDefinition $root): void
33
    {
34
        $root
35
            ->info('List of roles for queries and mutations')
36
            ->prototype('scalar')
37
            ->end();
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function configureEndpoint(Endpoint $endpoint): void
44
    {
45
        $endpoint->setQueries($this->secureDefinitions($endpoint->allQueries(), $endpoint));
46
    }
47
48
    /**
49
     * @param ExecutableDefinitionInterface[] $definitions
50
     * @param Endpoint                        $endpoint
51
     *
52
     * @return ExecutableDefinitionInterface[]
53
     */
54
    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

54
    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...
55
    {
56
        $secureDefinitions = [];
57
        foreach ($definitions as $definition) {
58
            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

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