Completed
Push — master ( 64d732...e25f3d )
by David
07:05
created

Configuration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 9
c 5
b 0
f 3
lcom 0
cbo 5
dl 0
loc 219
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 67 7
A configureClients() 0 20 1
B configurePlugins() 0 121 1
1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\ArrayNode;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
10
11
/**
12
 * This class contains the configuration information for the bundle.
13
 *
14
 * This information is solely responsible for how the different configuration
15
 * sections are normalized, and merged.
16
 *
17
 * @author David Buchmann <[email protected]>
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
class Configuration implements ConfigurationInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getConfigTreeBuilder()
26
    {
27
        $treeBuilder = new TreeBuilder();
28
        $rootNode = $treeBuilder->root('httplug');
29
30
        $this->configureClients($rootNode);
31
        $this->configurePlugins($rootNode);
32
33
        $rootNode
34
            ->validate()
35
                ->ifTrue(function ($v) {
36
                    return !empty($v['classes']['client'])
37
                        || !empty($v['classes']['message_factory'])
38
                        || !empty($v['classes']['uri_factory'])
39
                        || !empty($v['classes']['stream_factory']);
40
                })
41
                ->then(function ($v) {
42
                    foreach ($v['classes'] as $key => $class) {
43
                        if (null !== $class && !class_exists($class)) {
44
                            throw new InvalidConfigurationException(sprintf(
45
                                'Class %s specified for httplug.classes.%s does not exist.',
46
                                $class,
47
                                $key
48
                            ));
49
                        }
50
                    }
51
52
                    return $v;
53
                })
54
            ->end()
55
            ->children()
56
                ->arrayNode('main_alias')
57
                    ->addDefaultsIfNotSet()
58
                    ->info('Configure which service the main alias point to.')
59
                    ->children()
60
                        ->scalarNode('client')->defaultValue('httplug.client.default')->end()
61
                        ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end()
62
                        ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end()
63
                        ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end()
64
                    ->end()
65
                ->end()
66
                ->arrayNode('classes')
67
                    ->addDefaultsIfNotSet()
68
                    ->info('Overwrite a service class instead of using the discovery mechanism.')
69
                    ->children()
70
                        ->scalarNode('client')->defaultNull()->end()
71
                        ->scalarNode('message_factory')->defaultNull()->end()
72
                        ->scalarNode('uri_factory')->defaultNull()->end()
73
                        ->scalarNode('stream_factory')->defaultNull()->end()
74
                    ->end()
75
                ->end()
76
                ->arrayNode('toolbar')
77
                    ->addDefaultsIfNotSet()
78
                    ->info('Extend the debug profiler with inforation about requests.')
79
                    ->children()
80
                        ->enumNode('enabled')
81
                            ->info('If "auto" (default), the toolbar is activated when kernel.debug is true. You can force the toolbar on and off by changing this option.')
82
                            ->values([true, false, 'auto'])
83
                            ->defaultValue('auto')
84
                        ->end()
85
                        ->scalarNode('formatter')->defaultNull()->end()
86
                    ->end()
87
                ->end()
88
            ->end();
89
90
        return $treeBuilder;
91
    }
92
93
    protected function configureClients(ArrayNodeDefinition $root)
94
    {
95
        $root->children()
96
            ->arrayNode('clients')
97
                ->useAttributeAsKey('name')
98
                ->prototype('array')
99
                ->children()
100
                    ->scalarNode('factory')
101
                        ->isRequired()
102
                        ->cannotBeEmpty()
103
                        ->info('The service id of a factory to use when creating the adapter.')
104
                    ->end()
105
                    ->arrayNode('plugins')
106
                        ->info('A list of service ids of plugins. The order is important.')
107
                        ->prototype('scalar')->end()
108
                    ->end()
109
                    ->variableNode('config')->defaultValue([])->end()
110
                ->end()
111
            ->end();
112
    }
113
114
    /**
115
     * @param ArrayNodeDefinition $root
116
     */
117
    protected function configurePlugins(ArrayNodeDefinition $root)
118
    {
119
        $root->children()
120
            ->arrayNode('plugins')
121
                ->addDefaultsIfNotSet()
122
                ->children()
123
124
                    ->arrayNode('authentication')
125
                    ->canBeEnabled()
126
                        ->children()
127
                            ->scalarNode('authentication')
128
                                ->info('This must be a service id to a service implementing Http\Message\Authentication')
129
                                ->isRequired()
130
                                ->cannotBeEmpty()
131
                            ->end()
132
                        ->end()
133
                    ->end() // End authentication plugin
134
135
                    ->arrayNode('cache')
136
                    ->canBeEnabled()
137
                    ->addDefaultsIfNotSet()
138
                        ->children()
139
                            ->scalarNode('cache_pool')
140
                                ->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface')
141
                                ->isRequired()
142
                                ->cannotBeEmpty()
143
                            ->end()
144
                            ->scalarNode('stream_factory')
145
                                ->info('This must be a service id to a service implementing Http\Message\StreamFactory')
146
                                ->defaultValue('httplug.stream_factory')
147
                                ->cannotBeEmpty()
148
                            ->end()
149
                            ->arrayNode('config')
150
                                ->addDefaultsIfNotSet()
151
                                ->children()
152
                                    ->scalarNode('default_ttl')->defaultNull()->end()
153
                                    ->scalarNode('respect_cache_headers')->defaultTrue()->end()
154
                                ->end()
155
                            ->end()
156
                        ->end()
157
                    ->end() // End cache plugin
158
159
                    ->arrayNode('cookie')
160
                    ->canBeEnabled()
161
                        ->children()
162
                            ->scalarNode('cookie_jar')
163
                                ->info('This must be a service id to a service implementing Http\Message\CookieJar')
164
                                ->isRequired()
165
                                ->cannotBeEmpty()
166
                            ->end()
167
                        ->end()
168
                    ->end() // End cookie plugin
169
170
                    ->arrayNode('decoder')
171
                    ->canBeDisabled()
172
                    ->addDefaultsIfNotSet()
173
                        ->children()
174
                            ->scalarNode('use_content_encoding')->defaultTrue()->end()
175
                        ->end()
176
                    ->end() // End decoder plugin
177
178
                    ->arrayNode('history')
179
                    ->canBeEnabled()
180
                        ->children()
181
                            ->scalarNode('journal')
182
                                ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal')
183
                                ->isRequired()
184
                                ->cannotBeEmpty()
185
                            ->end()
186
                        ->end()
187
                    ->end() // End history plugin
188
189
                    ->arrayNode('logger')
190
                    ->canBeDisabled()
191
                    ->addDefaultsIfNotSet()
192
                        ->children()
193
                            ->scalarNode('logger')
194
                                ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface')
195
                                ->defaultValue('logger')
196
                                ->cannotBeEmpty()
197
                            ->end()
198
                            ->scalarNode('formatter')
199
                                ->info('This must be a service id to a service implementing Http\Message\Formatter')
200
                                ->defaultNull()
201
                            ->end()
202
                        ->end()
203
                    ->end() // End logger plugin
204
205
                    ->arrayNode('redirect')
206
                    ->canBeDisabled()
207
                    ->addDefaultsIfNotSet()
208
                        ->children()
209
                            ->scalarNode('preserve_header')->defaultTrue()->end()
210
                            ->scalarNode('use_default_for_multiple')->defaultTrue()->end()
211
                        ->end()
212
                    ->end() // End redirect plugin
213
214
                    ->arrayNode('retry')
215
                    ->canBeDisabled()
216
                    ->addDefaultsIfNotSet()
217
                        ->children()
218
                            ->scalarNode('retry')->defaultValue(1)->end()
219
                        ->end()
220
                    ->end() // End retry plugin
221
222
                    ->arrayNode('stopwatch')
223
                    ->canBeDisabled()
224
                    ->addDefaultsIfNotSet()
225
                        ->children()
226
                            ->scalarNode('stopwatch')
227
                                ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch')
228
                                ->defaultValue('debug.stopwatch')
229
                                ->cannotBeEmpty()
230
                            ->end()
231
                        ->end()
232
                    ->end() // End stopwatch plugin
233
234
                ->end()
235
            ->end()
236
        ->end();
237
    }
238
}
239