Configuration   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 5
c 8
b 1
f 2
lcom 0
cbo 3
dl 0
loc 97
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 29 4
A getConnectionsNode() 0 56 1
1
<?php
2
3
namespace Alawar\NginxPushStreamBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * This is the class that validates and merges configuration from your app/config files
11
 */
12
class Configuration implements ConfigurationInterface
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    public function getConfigTreeBuilder()
18
    {
19
        $treeBuilder = new TreeBuilder();
20
        $rootNode = $treeBuilder->root('nginx_push_stream');
21
22
        $rootNode
23
            ->beforeNormalization()
24
                ->ifTrue(function ($v) {
25
                    return is_array($v) && !array_key_exists('connections', $v) && !array_key_exists('connection', $v);
26
                })
27
                ->then(function ($v) {
28
                    // Key that should not be rewritten to the connection config
29
                    $excludedKeys = array('default_connection' => true);
30
                    $connection = array_diff_key($v, $excludedKeys);
31
                    $v = array_intersect_key($v, $excludedKeys);
32
                    $v['default_connection'] = isset($v['default_connection']) ? (string)$v['default_connection'] : 'default';
33
                    $v['connections'] = array($v['default_connection'] => $connection);
34
35
                    return $v;
36
                })
37
            ->end()
38
            ->children()
39
                ->scalarNode('default_connection')->end()
40
            ->end()
41
            ->append($this->getConnectionsNode())
42
        ->end();
43
44
        return $treeBuilder;
45
    }
46
47
    /**
48
     * Builds a node to handle connection configs
49
     *
50
     * @return ArrayNodeDefinition
51
     */
52
    public function getConnectionsNode()
53
    {
54
        $treeBuilder = new TreeBuilder();
55
        $node = $treeBuilder->root('connections');
56
57
        $node
58
            ->requiresAtLeastOneElement()
59
            ->useAttributeAsKey('name')
60
            ->fixXmlConfig('sub_url')
61
            ->fixXmlConfig('filter')
62
            ->performNoDeepMerging()
63
            ->prototype('array')
64
            ->children()
65
                ->scalarNode('pub_url')
66
                    ->isRequired()
67
                    ->example('http://example.com/pub/?id={token}')
68
                ->end()
69
                ->arrayNode('sub_urls')
70
                    ->isRequired()
71
                    ->prototype('scalar')
72
                    ->end()
73
                    ->example(array(
74
                        'polling' => 'http://example.com/sub-p/{tokens}',
75
                        'long-polling' => 'http://example.com/sub-lp/{tokens}',
76
                        'streaming' => 'http://example.com/sub-s/{tokens}',
77
                        'eventsource' => 'http://example.com/sub-ev/{tokens}',
78
                    ))
79
                ->end()
80
                ->scalarNode('id_generator')
81
                    ->defaultValue(true)
82
                    ->example(true)
83
                ->end()
84
                ->scalarNode('sender')
85
                    ->defaultValue(true)
86
                    ->example(true)
87
                ->end()
88
                ->arrayNode('filters')
89
                    ->performNoDeepMerging()
90
                    ->useAttributeAsKey('id')
91
                    ->prototype('array')
92
                        ->prototype('scalar')
93
                        ->end()
94
                        ->children()
95
                            ->scalarNode('class')->end()
96
                        ->end()
97
                    ->end()
98
                    ->example(array(
99
                        'hash' => array('class' => 'hash', 'secret' => 'mysecret', 'algo' => 'md5'),
100
                        'prefix' => array('class' => 'prefix', 'prefix' => 'myapp_'),
101
                    ))
102
                ->end()
103
            ->end()
104
        ;
105
106
        return $node;
107
    }
108
}
109