Completed
Push — master ( ca959b...d3c999 )
by Tomasz
06:06
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 48
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 41
cts 41
cp 1
rs 9.125
c 0
b 0
f 0
cc 2
eloc 41
nc 1
nop 0
crap 2
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 7
    public function __construct($alias)
28
    {
29 7
        $this->alias = $alias;
30 7
    }
31
32
    /**
33
     * Get configuration tree builder instance.
34
     *
35
     * @return TreeBuilder Tree builder instance.
36
     */
37 7
    public function getConfigTreeBuilder()
38
    {
39 7
        $treeBuilder = new TreeBuilder();
40 7
        $rootNode = $treeBuilder->root($this->alias);
41
        $rootNode
42 7
            ->validate()
43
                ->ifTrue(function($v) {
44 7
                    return !empty($v['enabled']) && empty($v['pools']);
45 7
                })
46 7
                ->thenInvalid('The child node "pools" at path "'.$this->alias.'" must be configured.')
47 7
            ->end()
48 7
            ->children()
49 7
                ->scalarNode('enabled')
50 7
                    ->defaultTrue()
51 7
                ->end()
52 7
                ->arrayNode('pools')
53 7
                    ->requiresAtLeastOneElement()
54 7
                    ->validate()
55
                    ->ifTrue(function (array $value) {
56 5
                            return !array_key_exists('default', $value);
57 7
                    })
58 7
                        ->thenInvalid('Default service not present')
59 7
                    ->end()
60 7
                    ->prototype('array')
61 7
                        ->children()
62 7
                            ->scalarNode('send_driver')
63 7
                                ->isRequired()
64 7
                                ->validate()
65 7
                                ->ifTrue(function ($value) {
66 5
                                        return !preg_match('/^@[a-zA-Z\.\-0-9\_]+$/', $value);
67 7
                                })
68 7
                                    ->thenInvalid('Malformed service ID "%s"')
69 7
                                ->end()
70 7
                            ->end()
71 7
                        ->end()
72 7
                    ->end()
73 7
                ->end()
74 7
                ->arrayNode('routes')
75 7
                    ->normalizeKeys(false)
76 7
                    ->prototype('scalar')
77 7
                        ->isRequired()
78 7
                    ->end()
79 7
                ->end()
80 7
            ->end()
81
            ;
82
83 7
        return $treeBuilder;
84
    }
85
}
86