Configuration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 55
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 48
cts 48
cp 1
rs 9.7692
c 0
b 0
f 0
cc 2
eloc 48
nc 1
nop 0
crap 2

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 Gendoria\CommandQueueBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
8
/**
9
 * This is the class that validates and merges configuration from your app/config files.
10
 *
11
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12
 */
13
class Configuration implements ConfigurationInterface
14
{
15
    /**
16
     * Configuration alias.
17
     *
18
     * @var string
19
     */
20
    private $alias;
21
22
    /**
23
     * Class constructor.
24
     *
25
     * @param string $alias Root configuration key.
26
     */
27 11
    public function __construct($alias)
28
    {
29 11
        $this->alias = $alias;
30 11
    }
31
32
    /**
33
     * Get configuration tree builder instance.
34
     *
35
     * @return TreeBuilder Tree builder instance.
36
     */
37 11
    public function getConfigTreeBuilder()
38
    {
39 11
        $treeBuilder = new TreeBuilder();
40 11
        $rootNode = $treeBuilder->root($this->alias);
41
        $rootNode
42 11
            ->validate()
43
                ->ifTrue(function($v) {
44 11
                    return !empty($v['enabled']) && empty($v['pools']);
45 11
                })
46 11
                ->thenInvalid('The child node "pools" at path "'.$this->alias.'" must be configured.')
47 11
            ->end()
48 11
            ->children()
49 11
                ->scalarNode('enabled')
50 11
                    ->defaultTrue()
51 11
                ->end()
52 11
                ->arrayNode('listeners')
53 11
                    ->addDefaultsIfNotSet()
54 11
                    ->children()
55 11
                        ->booleanNode('clear_entity_managers')->defaultTrue()->end()
56 11
                        ->booleanNode('clear_logs')->defaultTrue()->end()
57 11
                    ->end()
58 11
                ->end()
59 11
                ->arrayNode('pools')
60 11
                    ->requiresAtLeastOneElement()
61 11
                    ->validate()
62
                    ->ifTrue(function (array $value) {
63 9
                            return !array_key_exists('default', $value);
64 11
                    })
65 11
                        ->thenInvalid('Default service not present')
66 11
                    ->end()
67 11
                    ->prototype('array')
68 11
                        ->children()
69 11
                            ->scalarNode('send_driver')
70 11
                                ->isRequired()
71 11
                                ->validate()
72 11
                                ->ifTrue(function ($value) {
73 9
                                        return !preg_match('/^@[a-zA-Z\.\-0-9\_]+$/', $value);
74 11
                                })
75 11
                                    ->thenInvalid('Malformed service ID "%s"')
76 11
                                ->end()
77 11
                            ->end()
78 11
                        ->end()
79 11
                    ->end()
80 11
                ->end()
81 11
                ->arrayNode('routes')
82 11
                    ->normalizeKeys(false)
83 11
                    ->prototype('scalar')
84 11
                        ->isRequired()
85 11
                    ->end()
86 11
                ->end()
87 11
            ->end()
88
            ;
89
90 11
        return $treeBuilder;
91
    }
92
}
93