Configuration::getAmqpClientNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 31
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\DependencyInjection;
17
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
20
use Symfony\Component\Config\Definition\ConfigurationInterface;
21
22
/**
23
 * AppBundle configuration.
24
 */
25
class Configuration implements ConfigurationInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getConfigTreeBuilder()
31
    {
32
        $treeBuilder = new TreeBuilder();
33
        $rootNode    = $treeBuilder->root('veslo_app');
34
35
        $rootNode
36
            ->children()
37
                ->arrayNode('http')
38
                    ->children()
39
                        ->append($this->getHttpClientNode())
40
                        ->append($this->getHttpProxyNode())
41
                    ->end()
42
                    ->validate()
43
                        ->ifTrue(function ($v) {
44
                            if (empty($v['client']['proxy']['enabled'])) {
45
                                return false;
46
                            }
47
48
                            return !is_array($v['proxy']['static_list']) || !count($v['proxy']['static_list']);
49
                        })
50
                        ->thenInvalid('\'http.proxy.static_list\' must have at least one entry if proxy is enabled for the client')
51
                    ->end()
52
                ->end()
53
                ->append($this->getAmqpClientNode())
54
                ->arrayNode('workflow')
55
                    ->children()
56
                        ->arrayNode('vacancy_research')
57
                            ->children()
58
                                ->arrayNode('transitions')
59
                                    ->children()
60
                                        ->scalarNode('queue_prefix')
61
                                            ->isRequired()
62
                                            ->cannotBeEmpty()
63
                                        ->end()
64
                                    ->end()
65
                                ->end()
66
                            ->end()
67
                        ->end()
68
                    ->end()
69
                ->end()
70
                ->arrayNode('monolog')
71
                    ->children()
72
                        ->arrayNode('formatter')
73
                            ->children()
74
                                ->arrayNode('line')
75
                                    ->children()
76
                                        ->scalarNode('format')
77
                                            ->isRequired()
78
                                            ->cannotBeEmpty()
79
                                        ->end()
80
                                    ->end()
81
                                ->end()
82
                            ->end()
83
                        ->end()
84
                    ->end()
85
                ->end()
86
            ->end()
87
        ;
88
89
        return $treeBuilder;
90
    }
91
92
    /**
93
     * Returns a global HTTP client node
94
     *
95
     * @return ArrayNodeDefinition
96
     */
97
    private function getHttpClientNode(): ArrayNodeDefinition
98
    {
99
        $treeBuilder = new TreeBuilder();
100
        $node        = $treeBuilder->root('client');
101
102
        $node
103
            ->children()
104
                ->booleanNode('logging')
105
                    ->defaultFalse()
106
                ->end()
107
                ->arrayNode('headers')
108
                    ->children()
109
                        ->scalarNode('user_agent')
110
                            ->isRequired()
111
                            ->cannotBeEmpty()
112
                        ->end()
113
                    ->end()
114
                ->end()
115
                ->arrayNode('proxy')
116
                    ->info('Proxy options for the HTTP client')
117
                    ->canBeEnabled()
118
                ->end()
119
            ->end()
120
        ;
121
122
        return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Symfony\Component\Config...\Builder\NodeDefinition which includes types incompatible with the type-hinted return Symfony\Component\Config...der\ArrayNodeDefinition.
Loading history...
123
    }
124
125
    /**
126
     * Returns a proxy node for HTTP configuration tree
127
     *
128
     * @return ArrayNodeDefinition
129
     */
130
    private function getHttpProxyNode(): ArrayNodeDefinition
131
    {
132
        $treeBuilder = new TreeBuilder();
133
        $node        = $treeBuilder->root('proxy');
134
135
        $node
136
            ->children()
137
                ->arrayNode('dynamic')
138
                    ->children()
139
                        ->scalarNode('fetch_uri')
140
                            ->info('URI of proxy list from an external source for rotation')
141
                            ->example('https://proxy-provider.ltd/my_proxy_list')
142
                            ->treatFalseLike('')
143
                        ->end()
144
                        ->scalarNode('format')
145
                            ->info('Format in which proxy list are stored')
146
                            ->example('json')
147
                        ->end()
148
                        ->arrayNode('decoder_context')
149
                            ->info('Options that decoder have access to')
150
                            ->scalarPrototype()->end()
151
                        ->end()
152
                        ->arrayNode('cache')
153
                            ->info('Cache options for proxy list received by URI')
154
                            ->canBeEnabled()
155
                            ->children()
156
                                ->scalarNode('key')->end()
157
                                ->scalarNode('lifetime')
158
                                    ->info('Time for which a cached proxy list is considered to be valid')
159
                                ->end()
160
                            ->end()
161
                        ->end()
162
                    ->end()
163
                    ->validate()
164
                        ->ifTrue(function ($v) {
165
                            return !empty($v['fetch_uri']) && empty($v['format']);
166
                        })
167
                        ->thenInvalid('Resource format must also be configured if \'fetch_uri\' is present %s')
168
                    ->end()
169
                ->end()
170
                ->arrayNode('static_list')
171
                    ->info('Is used by a default proxy locator (or fallback) if no others is present')
172
                    ->scalarPrototype()->end()
173
                ->end()
174
            ->end()
175
        ;
176
177
        return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Symfony\Component\Config...\Builder\NodeDefinition which includes types incompatible with the type-hinted return Symfony\Component\Config...der\ArrayNodeDefinition.
Loading history...
178
    }
179
180
    /**
181
     * Returns a global amqp client node
182
     *
183
     * @return ArrayNodeDefinition
184
     */
185
    private function getAmqpClientNode(): ArrayNodeDefinition
186
    {
187
        $treeBuilder = new TreeBuilder();
188
        $node        = $treeBuilder->root('amqp_client');
189
190
        $node
191
            ->children()
192
                ->scalarNode('host')
193
                    ->isRequired()
194
                    ->cannotBeEmpty()
195
                ->end()
196
                ->scalarNode('vhost')
197
                    ->isRequired()
198
                    ->cannotBeEmpty()
199
                ->end()
200
                ->scalarNode('user')
201
                    ->isRequired()
202
                    ->cannotBeEmpty()
203
                ->end()
204
                ->scalarNode('password')
205
                    ->isRequired()
206
                    ->cannotBeEmpty()
207
                ->end()
208
                ->scalarNode('heartbeat')
209
                    ->isRequired()
210
                    ->cannotBeEmpty()
211
                ->end()
212
            ->end()
213
        ;
214
215
        return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Symfony\Component\Config...\Builder\NodeDefinition which includes types incompatible with the type-hinted return Symfony\Component\Config...der\ArrayNodeDefinition.
Loading history...
216
    }
217
}
218