Completed
Pull Request — master (#5)
by Pavel
04:05
created

Configuration::configureRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 57
ccs 45
cts 47
cp 0.9574
rs 9.6818
cc 1
eloc 41
nc 1
nop 1
crap 1

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
 * 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 1
        $proto->append(
72 1
            (new VariableNodeDefinition('context'))
73 1
                ->beforeNormalization()
74 1
                ->ifString()
75 1
                ->then(
76 1
                    function ($v) {
77 1
                        return [$v];
78
                    }
79 1
                )
80 1
                ->end()
81 1
                ->defaultValue(['Default'])
82 1
                ->info('Endpoint-wide context')
83 1
                ->example(['Default'])
84 1
        );
85
86 1
        $endpoints->useAttributeAsKey('name');
87 1
        $proto->addDefaultsIfNotSet();
88 1
    }
89
}
90