Completed
Push — master ( 971421...d8f9fb )
by Matthew
09:04
created

Configuration::addRabbitMq()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 38
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 27
cts 27
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 30
nc 1
nop 0
crap 5
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection;
4
5
use Dtc\QueueBundle\Model\PriorityJobManager;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
class Configuration implements ConfigurationInterface
10
{
11
    /**
12
     * Generates the configuration tree.
13
     *
14
     * @return TreeBuilder
15
     */
16 4
    public function getConfigTreeBuilder()
17
    {
18 4
        $treeBuilder = new TreeBuilder();
19 4
        $rootNode = $treeBuilder->root('dtc_queue');
20
21
        $rootNode
22 4
            ->children()
23 4
                ->scalarNode('document_manager')
24 4
                    ->defaultValue('default')
25 4
                    ->cannotBeEmpty()
26 4
                ->end()
27 4
                ->scalarNode('entity_manager')
28 4
                    ->defaultValue('default')
29 4
                    ->cannotBeEmpty()
30 4
                ->end()
31 4
                ->scalarNode('default_manager')
32 4
                    ->defaultValue('mongodb')
33 4
                    ->cannotBeEmpty()
34 4
                ->end()
35 4
                ->scalarNode('run_manager')
36 4
                ->end()
37 4
                ->scalarNode('document_am')
38 4
                ->end()
39 4
                ->scalarNode('class_job')
40 4
                ->end()
41 4
                ->scalarNode('class_job_archive')
42 4
                ->end()
43 4
                ->scalarNode('class_job_timing')
44 4
                ->end()
45 4
                ->scalarNode('class_run')
46 4
                ->end()
47 4
                ->scalarNode('class_run_archive')
48 4
                ->end()
49 4
                ->booleanNode('record_timings')
50 4
                    ->defaultFalse()
51 4
                ->end()
52 4
                ->integerNode('priority_max')
53 4
                    ->defaultValue(255)
54 4
                    ->min(1)
55 4
                ->end()
56 4
                ->enumNode('priority_direction')
57 4
                    ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC])
58 4
                    ->defaultValue(PriorityJobManager::PRIORITY_DESC)
59 4
                ->end()
60 4
                ->arrayNode('beanstalkd')
61 4
                    ->children()
62 4
                        ->scalarNode('host')->end()
63 4
                        ->scalarNode('tube')->end()
64 4
                    ->end()
65 4
                ->end()
66 4
                ->append($this->addRabbitMq())
67 4
                ->append($this->addAdmin())
68 4
            ->end();
69
70 4
        return $treeBuilder;
71
    }
72
73 4
    protected function addAdmin()
74
    {
75 4
        $treeBuilder = new TreeBuilder();
76 4
        $rootNode = $treeBuilder->root('admin');
77
        $rootNode
78 4
            ->addDefaultsIfNotSet()
79 4
            ->children()
80 4
                ->scalarNode('chartjs')->defaultValue('https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js')->end()
81 4
            ->end();
82
83 4
        return $rootNode;
84
    }
85
86 4
    protected function addRabbitMqOptions()
87
    {
88 4
        $treeBuilder = new TreeBuilder();
89 4
        $rootNode = $treeBuilder->root('options');
90
        $rootNode
91 4
            ->children()
92 4
                ->scalarNode('insist')->end()
93 4
                ->scalarNode('login_method')->end()
94 4
                ->scalarNode('login_response')->end()
95 4
                ->scalarNode('locale')->end()
96 4
                ->floatNode('connection_timeout')->end()
97 4
                ->floatNode('read_write_timeout')->end()
98 4
                ->booleanNode('keepalive')->end()
99 4
                ->integerNode('heartbeat')->end()
100 4
            ->end();
101
102 4
        return $rootNode;
103
    }
104
105 4
    protected function addRabbitMqSslOptions()
106
    {
107 4
        $treeBuilder = new TreeBuilder();
108 4
        $rootNode = $treeBuilder->root('ssl_options');
109
        $rootNode
110 4
            ->prototype('variable')->end()
111 4
            ->validate()
112 4
                ->ifTrue(function ($node) {
113 1
                    if (!is_array($node)) {
114
                        return true;
115
                    }
116 1
                    foreach ($node as $key => $value) {
117 1
                        if (is_array($value)) {
118 1
                            if ('peer_fingerprint' !== $key) {
119 1
                                return true;
120
                            } else {
121 1
                                foreach ($value as $key1 => $value1) {
122 1
                                    if (!is_string($key1) || !is_string($value1)) {
123 1
                                        return true;
124
                                    }
125
                                }
126
                            }
127
                        }
128
                    }
129
130 1
                    return false;
131 4
                })
132 4
                ->thenInvalid('Must be key-value pairs')
133 4
            ->end();
134
135 4
        return $rootNode;
136
    }
137
138 4 View Code Duplication
    protected function addRabbitMqExchange()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140 4
        $treeBuilder = new TreeBuilder();
141 4
        $rootNode = $treeBuilder->root('exchange_args');
142
        $rootNode
143 4
            ->addDefaultsIfNotSet()
144 4
            ->children()
145 4
                ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
146 4
                ->booleanNode('type')->defaultValue('direct')->end()
147 4
                ->booleanNode('passive')->defaultFalse()->end()
148 4
                ->booleanNode('durable')->defaultTrue()->end()
149 4
                ->booleanNode('auto_delete')->defaultFalse()->end()
150 4
            ->end();
151
152 4
        return $rootNode;
153
    }
154
155 4 View Code Duplication
    protected function addRabbitMqArgs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157 4
        $treeBuilder = new TreeBuilder();
158 4
        $rootNode = $treeBuilder->root('queue_args');
159
        $rootNode
160 4
            ->addDefaultsIfNotSet()
161 4
            ->children()
162 4
                ->scalarNode('queue')->defaultValue('dtc_queue')->end()
163 4
                ->booleanNode('passive')->defaultFalse()->end()
164 4
                ->booleanNode('durable')->defaultTrue()->end()
165 4
                ->booleanNode('exclusive')->defaultFalse()->end()
166 4
                ->booleanNode('auto_delete')->defaultFalse()->end()
167 4
            ->end();
168
169 4
        return $rootNode;
170
    }
171
172 4
    protected function addRabbitMq()
173
    {
174 4
        $treeBuilder = new TreeBuilder();
175 4
        $rootNode = $treeBuilder->root('rabbit_mq');
176
        $rootNode
177 4
            ->children()
178 4
                ->scalarNode('host')->end()
179 4
                ->scalarNode('port')->end()
180 4
                ->scalarNode('user')->end()
181 4
                ->scalarNode('password')->end()
182 4
                ->scalarNode('vhost')->defaultValue('/')->end()
183 4
                ->booleanNode('ssl')->defaultFalse()->end()
184 4
            ->end()
185 4
            ->append($this->addRabbitMqOptions())
186 4
            ->append($this->addRabbitMqSslOptions())
187 4
            ->append($this->addRabbitMqArgs())
188 4
            ->append($this->addRabbitMqExchange())
189 4
            ->validate()->always(function ($node) {
190 1
                if (empty($node['ssl_options'])) {
191 1
                    unset($node['ssl_options']);
192
                }
193 1
                if (empty($node['options'])) {
194 1
                    unset($node['options']);
195
                }
196
197 1
                return $node;
198 4
            })->end()
199 4
            ->validate()->ifTrue(function ($node) {
200 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
201 1
                    return true;
202
                }
203
204 1
                return false;
205 4
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
206
            ->end();
207
208
        return $rootNode;
209
    }
210
}
211