Completed
Pull Request — master (#37)
by Tobias
72:36 queued 66:40
created

Configuration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 214
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 214
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 67 7
A configureClients() 0 20 1
B configurePlugins() 0 116 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')->cannotBeEmpty()->defaultValue('httplug.stream_factory')->end()
145
                            ->arrayNode('config')
146
                                ->children()
147
                                    ->scalarNode('default_ttl')->defaultNull()->end()
148
                                    ->scalarNode('respect_cache_headers')->defaultTrue()->end()
149
                                ->end()
150
                            ->end()
151
                        ->end()
152
                    ->end() // End cache plugin
153
154
                    ->arrayNode('cookie')
155
                    ->canBeEnabled()
156
                        ->children()
157
                            ->scalarNode('cookie_jar')
158
                                ->info('This must be a service id to a service implementing Http\Message\CookieJar')
159
                                ->isRequired()
160
                                ->cannotBeEmpty()
161
                            ->end()
162
                        ->end()
163
                    ->end() // End cookie plugin
164
165
                    ->arrayNode('decoder')
166
                    ->canBeDisabled()
167
                    ->addDefaultsIfNotSet()
168
                        ->children()
169
                            ->scalarNode('use_content_encoding')->defaultTrue()->end()
170
                        ->end()
171
                    ->end() // End decoder plugin
172
173
                    ->arrayNode('history')
174
                    ->canBeEnabled()
175
                        ->children()
176
                            ->scalarNode('journal')
177
                                ->info('This must be a service id to a service implementing Http\Client\Plugin\Journal')
178
                                ->isRequired()
179
                                ->cannotBeEmpty()
180
                            ->end()
181
                        ->end()
182
                    ->end() // End history plugin
183
184
                    ->arrayNode('logger')
185
                    ->canBeDisabled()
186
                    ->addDefaultsIfNotSet()
187
                        ->children()
188
                            ->scalarNode('logger')
189
                                ->info('This must be a service id to a service implementing Psr\Log\LoggerInterface')
190
                                ->defaultValue('logger')
191
                                ->cannotBeEmpty()
192
                            ->end()
193
                            ->scalarNode('formatter')
194
                                ->info('This must be a service id to a service implementing Http\Message\Formatter')
195
                                ->defaultNull()
196
                            ->end()
197
                        ->end()
198
                    ->end() // End logger plugin
199
200
                    ->arrayNode('redirect')
201
                    ->canBeDisabled()
202
                    ->addDefaultsIfNotSet()
203
                        ->children()
204
                            ->scalarNode('preserve_header')->defaultTrue()->end()
205
                            ->scalarNode('use_default_for_multiple')->defaultTrue()->end()
206
                        ->end()
207
                    ->end() // End redirect plugin
208
209
                    ->arrayNode('retry')
210
                    ->canBeDisabled()
211
                    ->addDefaultsIfNotSet()
212
                        ->children()
213
                            ->scalarNode('retry')->defaultValue(1)->end()
214
                        ->end()
215
                    ->end() // End retry plugin
216
217
                    ->arrayNode('stopwatch')
218
                    ->canBeDisabled()
219
                    ->addDefaultsIfNotSet()
220
                        ->children()
221
                            ->scalarNode('stopwatch')
222
                                ->info('This must be a service id to a service extending Symfony\Component\Stopwatch\Stopwatch')
223
                                ->defaultValue('debug.stopwatch')
224
                                ->cannotBeEmpty()
225
                            ->end()
226
                        ->end()
227
                    ->end() // End stopwatch plugin
228
229
                ->end()
230
            ->end()
231
        ->end();
232
    }
233
}
234