Completed
Push — master ( f2d7cc...c90f48 )
by Robert
04:06
created

Configuration::addDebugNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MediaMonks\RestApiBundle\DependencyInjection;
4
5
use MediaMonks\RestApiBundle\Request\Format;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
final class Configuration implements ConfigurationInterface
11
{
12
    /**
13
     * Generates the configuration tree.
14
     *
15
     * @return TreeBuilder
16
     */
17 3
    public function getConfigTreeBuilder()
18
    {
19 3
        $treeBuilder = new TreeBuilder();
20 3
        $rootNode = $treeBuilder->root('mediamonks_rest_api');
21
22 3
        $this->addDebugNode($rootNode);
23 3
        $this->addRequestMatcherNode($rootNode);
24 3
        $this->addOutputFormatNode($rootNode);
25 3
        $this->addPostMessageOriginNode($rootNode);
26
27 3
        return $treeBuilder;
28
    }
29
30
    /**
31
     * @param ArrayNodeDefinition $node
32
     */
33 3
    private function addDebugNode(ArrayNodeDefinition $node)
34
    {
35 3
        $node->children()
36 3
            ->scalarNode('debug')
37 3
            ->defaultNull()
38 3
            ->end();
39 3
    }
40
41
    /**
42
     * @param ArrayNodeDefinition $node
43
     */
44 3
    private function addRequestMatcherNode(ArrayNodeDefinition $node)
45
    {
46 3
        $node->children()
47 3
            ->arrayNode('request_matcher')
48 3
                ->addDefaultsIfNotSet()
49 3
                    ->children()
50 3
                        ->arrayNode('whitelist')
51 3
                            ->defaultValue(['~^/api/$~', '~^/api~'])
52 3
                            ->prototype('scalar')
53 3
                            ->end()
54 3
                        ->end()
55 3
                        ->arrayNode('blacklist')
56 3
                            ->defaultValue(['~^/api/doc~'])
57 3
                            ->prototype('scalar')
58 3
                        ->end()
59 3
                    ->end()
60 3
                ->end()
61 3
            ->end();
62 3
    }
63
64
    /**
65
     * @param ArrayNodeDefinition $node
66
     */
67 3
    private function addOutputFormatNode(ArrayNodeDefinition $node)
68
    {
69 3
        $node->children()
70 3
            ->arrayNode('output_formats')
71 3
                ->defaultValue([Format::getDefault()])
72 3
                ->prototype('scalar')
73 3
                ->validate()
74 3
                    ->ifNotInArray(Format::getAvailable())
75 3
                    ->thenInvalid('Formats can only contain "' . implode('"', Format::getAvailable()) . '", not "%s"')
76 3
                    ->end()
77 3
                ->end()
78 3
            ->end();
79 3
    }
80
81
    /**
82
     * @param ArrayNodeDefinition $node
83
     */
84 3
    private function addPostMessageOriginNode(ArrayNodeDefinition $node)
85
    {
86 3
        $node->children()
87 3
            ->scalarNode('post_message_origin')
88 3
            ->defaultNull()
89 3
            ->end();
90 3
    }
91
}
92