Completed
Push — master ( 4e378a...cfdcd7 )
by Yann
05:10
created

Configuration::getPermissionsNode()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 57
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 45
nc 1
nop 0

How to fix   Long Method   

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
namespace Yokai\SecurityExtraBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * @author Yann Eugoné <[email protected]>
11
 */
12
class Configuration implements ConfigurationInterface
13
{
14
    /**
15
     * @inheritDoc
16
     */
17
    public function getConfigTreeBuilder()
18
    {
19
        $tree = new TreeBuilder();
20
        $root = $tree->root('yokai_security_extra');
21
22
        $root
23
            ->children()
24
                ->append($this->getPermissionsNode())
25
            ->end()
26
        ;
27
28
        return $tree;
29
    }
30
31
    /**
32
     * @return ArrayNodeDefinition
33
     */
34
    private function getPermissionsNode()
35
    {
36
        $isString = function ($value) {
37
            return is_string($value);
38
        };
39
        $isNotCollection = function ($value) {
40
            if (!is_array($value)) {
41
                return true;
42
            }
43
44
            if (isset($value[0]) && is_string($value[0]) && isset($value[1]) && is_string($value[1])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return isset($value[0]) ...& is_string($value[1]);.
Loading history...
45
                return true;
46
            }
47
48
            return false;
49
        };
50
        $toArray = function ($value) {
51
            return [$value];
52
        };
53
54
        ($node = $this->root('permissions'))
55
            ->prototype('array')
56
                ->children()
57
                    ->arrayNode('attributes')
58
                        ->info('Matching attribute(s). Empty means all attributes.')
59
                        ->prototype('scalar')->end()
60
                        ->beforeNormalization()
61
                            ->ifTrue($isString)->then($toArray)
62
                        ->end()
63
                    ->end()
64
                    ->arrayNode('subjects')
65
                        ->info('Matching subject(s) types. Can be either classes, interfaces or types. Empty means all subjects.')
66
                        ->prototype('scalar')->end()
67
                        ->beforeNormalization()
68
                            ->ifTrue($isString)->then($toArray)
69
                        ->end()
70
                    ->end()
71
                    ->arrayNode('roles')
72
                        ->info('Required role(s) for these attributes & subjects.')
73
                        ->prototype('scalar')->end()
74
                        ->beforeNormalization()
75
                            ->ifTrue($isString)->then($toArray)
76
                        ->end()
77
                    ->end()
78
                    ->arrayNode('callables')
79
                        ->info('Callables that will verify access.')
80
                        ->prototype('variable')->end()
81
                        ->beforeNormalization()
82
                            ->ifTrue($isNotCollection)->then($toArray)
83
                        ->end()
84
                    ->end()
85
                ->end()
86
            ->end()
87
        ;
88
89
        return $node;
90
    }
91
92
    /**
93
     * @param string $name
94
     *
95
     * @return ArrayNodeDefinition
96
     */
97
    private function root($name)
98
    {
99
        return (new TreeBuilder())->root($name);
100
    }
101
}
102