Completed
Push — master ( 055f1b...f72a4b )
by Pavel
52s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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