Passed
Pull Request — master (#9)
by Pavel
06:14
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.064

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 10
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1.064
1
<?php
2
3
namespace Bankiru\Api\Rpc\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
final class Configuration implements ConfigurationInterface
12
{
13
    /**
14
     * Generates the configuration tree builder.
15
     *
16
     * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
17
     */
18 1
    public function getConfigTreeBuilder()
19
    {
20 1
        $builder = new TreeBuilder();
21 1
        $root    = $builder->root('rpc_server');
22
23 1
        $root->children()->booleanNode('debug')->defaultValue('%kernel.debug%');
24
25 1
        $this->configureRouter($root->children()->arrayNode('router'));
26
27 1
        return $builder;
28
    }
29
30 2
    private function configureRouter(ArrayNodeDefinition $root)
31
    {
32 2
        $root->addDefaultsIfNotSet();
33 2
        $root->treatNullLike(['endpoints' => []]);
34
        /** @var ArrayNodeDefinition $proto */
35 1
        $endpoints = $root->children()->arrayNode('endpoints');
36 1
37 2
        $proto = $endpoints->prototype('array');
38 2
39 2
        $proto->append(
40 3
            (new ScalarNodeDefinition('path'))
41 3
                ->isRequired()
42 2
                ->example('/')
43 2
                ->cannotBeEmpty()
44 2
                ->info('Endpoint URI')
45 2
        );
46 2
        $proto->append(
47 2
            (new ArrayNodeDefinition('resources'))
48 3
                ->beforeNormalization()
49 2
                ->ifNull()
50 3
                ->then(
51 2
                    function () {
52 1
                        return [];
53 1
                    }
54 3
                )
55 2
                ->ifString()
56 2
                ->then(
57 1
                    function ($v) {
58 1
                        return [$v];
59 1
                    }
60 3
                )
61 3
                ->end()
62 3
                ->prototype('scalar')->end()
63 3
                ->example('rpc.yml')
64 3
                ->info('Route definitions')
65 3
        );
66 3
        $proto->append(
67 3
            (new VariableNodeDefinition('defaults'))
68 3
        );
69 3
        $proto->append(
70 2
            (new VariableNodeDefinition('context'))
71 2
                ->beforeNormalization()
72 2
                ->ifString()
73 2
                ->then(
74 3
                    function ($v) {
75 2
                        return [$v];
76 2
                    }
77 3
                )
78 3
                ->end()
79 2
                ->defaultValue(['Default'])
80 2
                ->info('Endpoint-wide context')
81 2
                ->example(['Default'])
82 2
        );
83 1
84 1
        $endpoints->useAttributeAsKey('name');
85 1
        $proto->addDefaultsIfNotSet();
86 2
    }
87
}
88