Passed
Push — master ( 4d8734...991786 )
by Matthew
09:35 queued 06:44
created

RabbitMQConfiguration::addRabbitMqSslOptions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 0
dl 0
loc 18
ccs 12
cts 13
cp 0.9231
crap 2.0017
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
7
trait RabbitMQConfiguration
8
{
9 9
    protected function addRabbitMqOptions()
10
    {
11 9
        $treeBuilder = new TreeBuilder();
12 9
        $rootNode = $treeBuilder->root('options');
13
        $rootNode
14 9
            ->children()
15 9
            ->scalarNode('insist')->end()
16 9
            ->scalarNode('login_method')->end()
17 9
            ->scalarNode('login_response')->end()
18 9
            ->scalarNode('locale')->end()
19 9
            ->floatNode('connection_timeout')->end()
20 9
            ->floatNode('read_write_timeout')->end()
21 9
            ->booleanNode('keepalive')->end()
22 9
            ->integerNode('heartbeat')->end()
23 9
            ->end();
24
25 9
        return $rootNode;
26
    }
27
28 9
    protected function addRabbitMqSslOptions()
29
    {
30 9
        $treeBuilder = new TreeBuilder();
31 9
        $rootNode = $treeBuilder->root('ssl_options');
32
        $rootNode
33 9
            ->prototype('variable')->end()
0 ignored issues
show
Bug introduced by
The method prototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            ->/** @scrutinizer ignore-call */ 
34
              prototype('variable')->end()
Loading history...
34 9
            ->validate()
35 9
            ->ifTrue(function ($node) {
36 1
                if (!is_array($node)) {
37
                    return true;
38
                }
39
40 1
                return $this->validateNode($node);
41 9
            })
42 9
            ->thenInvalid('Must be key-value pairs')
43 9
            ->end();
44
45 9
        return $rootNode;
46
    }
47
48 1
    private function validateNode(array $node)
49
    {
50 1
        foreach ($node as $key => $value) {
51 1
            if (is_array($value)) {
52 1
                if ($this->validateArray($key, $value)) {
53 1
                    return true;
54
                }
55
            }
56
        }
57
58 1
        return false;
59
    }
60
61 1
    private function validateArray($key, $value)
62
    {
63 1
        if ('peer_fingerprint' !== $key) {
64 1
            return true;
65
        }
66 1
        foreach ($value as $key1 => $value1) {
67 1
            if (!is_string($key1) || !is_string($value1)) {
68 1
                return true;
69
            }
70
        }
71
72 1
        return false;
73
    }
74
75 9
    protected function addRabbitMqExchange()
76
    {
77 9
        $treeBuilder = new TreeBuilder();
78 9
        $rootNode = $treeBuilder->root('exchange_args');
79
        $rootNode
80 9
            ->addDefaultsIfNotSet()
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            ->/** @scrutinizer ignore-call */ 
81
              addDefaultsIfNotSet()
Loading history...
81 9
            ->children()
82 9
            ->scalarNode('exchange')->defaultValue('dtc_queue_exchange')->end()
83 9
            ->booleanNode('type')->defaultValue('direct')->end()
84 9
            ->booleanNode('passive')->defaultFalse()->end()
85 9
            ->booleanNode('durable')->defaultTrue()->end()
86 9
            ->booleanNode('auto_delete')->defaultFalse()->end()
87 9
            ->end();
88
89 9
        return $rootNode;
90
    }
91
92 9
    protected function addRabbitMqArgs()
93
    {
94 9
        $treeBuilder = new TreeBuilder();
95 9
        $rootNode = $treeBuilder->root('queue_args');
96
        $rootNode
97 9
            ->addDefaultsIfNotSet()
98 9
            ->children()
99 9
            ->scalarNode('queue')->defaultValue('dtc_queue')->end()
100 9
            ->booleanNode('passive')->defaultFalse()->end()
101 9
            ->booleanNode('durable')->defaultTrue()->end()
102 9
            ->booleanNode('exclusive')->defaultFalse()->end()
103 9
            ->booleanNode('auto_delete')->defaultFalse()->end()
104 9
            ->end();
105
106 9
        return $rootNode;
107
    }
108
109 9
    protected function addRabbitMq()
110
    {
111 9
        $treeBuilder = new TreeBuilder();
112 9
        $rootNode = $treeBuilder->root('rabbit_mq');
113
        $rootNode
114 9
            ->children()
115 9
            ->scalarNode('host')->end()
116 9
            ->scalarNode('port')->end()
117 9
            ->scalarNode('user')->end()
118 9
            ->scalarNode('password')->end()
119 9
            ->scalarNode('vhost')->defaultValue('/')->end()
120 9
            ->booleanNode('ssl')->defaultFalse()->end()
121 9
            ->append($this->addRabbitMqOptions())
122 9
            ->append($this->addRabbitMqSslOptions())
123 9
            ->append($this->addRabbitMqArgs())
124 9
            ->append($this->addRabbitMqExchange())
125 9
            ->end()
126 9
            ->validate()->always(function ($node) {
127 1
                if (empty($node['ssl_options'])) {
128 1
                    unset($node['ssl_options']);
129
                }
130 1
                if (empty($node['options'])) {
131 1
                    unset($node['options']);
132
                }
133
134 1
                return $node;
135 9
            })->end()
136 9
            ->validate()->ifTrue(function ($node) {
137 1
                if (isset($node['ssl_options']) && !$node['ssl']) {
138 1
                    return true;
139
                }
140
141 1
                return false;
142 9
            })->thenInvalid('ssl must be true in order to set ssl_options')->end()
143
            ->end();
144
145
        return $rootNode;
146
    }
147
}
148