AccessControlDefinitionPlugin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 53.85%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 13
c 2
b 1
f 0
dl 0
loc 28
ccs 7
cts 13
cp 0.5385
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 3
A buildConfig() 0 8 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\Definition\Plugin;
12
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
use Symfony\Component\ExpressionLanguage\ParsedExpression;
15
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
16
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
18
19
/**
20
 * Compiles the expression used in AccessControl annotation to check later in AccessControlListener
21
 */
22
class AccessControlDefinitionPlugin extends AbstractDefinitionPlugin
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function buildConfig(ArrayNodeDefinition $root): void
28
    {
29
        $config = $root
30
            ->info('Control the access to fields and objects')
31
            ->children();
32
33
        $config->scalarNode('expression');
34
        $config->scalarNode('message');
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 1
    public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config): void
41
    {
42 1
        if ($config && $expression = $config['expression']) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $nodes =
44 1
                (new ExpressionLanguage())
45 1
                    ->parse($expression, ['token', 'user', 'object', 'roles', 'request', 'trust_resolver'])
46 1
                    ->getNodes();
47
48 1
            $config['expression_serialized'] = serialize(new ParsedExpression($expression, $nodes));
49 1
            $definition->setMeta($this->getName(), $config);
50
        }
51 1
    }
52
}
53