Completed
Pull Request — master (#37)
by Tobias
24:24 queued 14:34
created

Configuration::configurePlugins()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 87
rs 8.6296
cc 1
eloc 75
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 */
19
class Configuration implements ConfigurationInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getConfigTreeBuilder()
25
    {
26
        $treeBuilder = new TreeBuilder();
27
        $rootNode = $treeBuilder->root('httplug');
28
29
        $this->configureClients($rootNode);
30
        $this->configurePlugins($rootNode);
31
32
        $rootNode
33
            ->validate()
34
                ->ifTrue(function ($v) {
35
                    return !empty($v['classes']['client'])
36
                        || !empty($v['classes']['message_factory'])
37
                        || !empty($v['classes']['uri_factory'])
38
                        || !empty($v['classes']['stream_factory']);
39
                })
40
                ->then(function ($v) {
41
                    foreach ($v['classes'] as $key => $class) {
42
                        if (null !== $class && !class_exists($class)) {
43
                            throw new InvalidConfigurationException(sprintf(
44
                                'Class %s specified for httplug.classes.%s does not exist.',
45
                                $class,
46
                                $key
47
                            ));
48
                        }
49
                    }
50
51
                    return $v;
52
                })
53
            ->end()
54
            ->children()
55
                ->arrayNode('main_alias')
56
                    ->addDefaultsIfNotSet()
57
                    ->info('Configure which service the main alias point to.')
58
                    ->children()
59
                        ->scalarNode('client')->defaultValue('httplug.client.default')->end()
60
                        ->scalarNode('message_factory')->defaultValue('httplug.message_factory.default')->end()
61
                        ->scalarNode('uri_factory')->defaultValue('httplug.uri_factory.default')->end()
62
                        ->scalarNode('stream_factory')->defaultValue('httplug.stream_factory.default')->end()
63
                    ->end()
64
                ->end()
65
                ->arrayNode('classes')
66
                    ->addDefaultsIfNotSet()
67
                    ->info('Overwrite a service class instead of using the discovery mechanism.')
68
                    ->children()
69
                        ->scalarNode('client')->defaultNull()->end()
70
                        ->scalarNode('message_factory')->defaultNull()->end()
71
                        ->scalarNode('uri_factory')->defaultNull()->end()
72
                        ->scalarNode('stream_factory')->defaultNull()->end()
73
                    ->end()
74
                ->end()
75
                ->arrayNode('toolbar')
76
                    ->addDefaultsIfNotSet()
77
                    ->info('Extend the debug profiler with inforation about requests.')
78
                    ->children()
79
                        ->enumNode('enabled')
80
                            ->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.')
81
                            ->values([true, false, 'auto'])
82
                            ->defaultValue('auto')
83
                        ->end()
84
                        ->scalarNode('formatter')->defaultNull()->end()
85
                    ->end()
86
                ->end()
87
            ->end();
88
89
        return $treeBuilder;
90
    }
91
92
    protected function configureClients(ArrayNodeDefinition $root)
93
    {
94
        $root->children()
95
            ->arrayNode('clients')
96
                ->useAttributeAsKey('name')
97
                ->prototype('array')
98
                ->children()
99
                    ->scalarNode('factory')
100
                        ->isRequired()
101
                        ->cannotBeEmpty()
102
                        ->info('The service id of a factory to use when creating the adapter.')
103
                    ->end()
104
                    ->arrayNode('plugins')
105
                        ->info('A list of service ids of plugins. The order is important.')
106
                        ->prototype('scalar')->end()
107
                    ->end()
108
                    ->variableNode('config')->defaultValue([])->end()
109
                ->end()
110
            ->end();
111
    }
112
113
    /**
114
     * @param ArrayNodeDefinition $root
115
     */
116
    protected function configurePlugins(ArrayNodeDefinition $root)
117
    {
118
        $root->children()
119
            ->arrayNode('plugins')
120
                ->addDefaultsIfNotSet()
121
                ->children()
122
123
                    ->arrayNode('authentication')
124
                    ->canBeEnabled()
125
                        ->children()
126
                            ->scalarNode('authentication')->isRequired()->cannotBeEmpty()->end()
127
                        ->end()
128
                    ->end() // End authentication plugin
129
130
                    ->arrayNode('cache')
131
                    ->canBeEnabled()
132
                    ->addDefaultsIfNotSet()
133
                        ->children()
134
                            ->scalarNode('cache_pool')->isRequired()->cannotBeEmpty()->end()
135
                            ->scalarNode('stream_factory')->cannotBeEmpty()->defaultValue('httplug.stream_factory')->end()
136
                            ->arrayNode('config')
137
                                ->children()
138
                                    ->scalarNode('default_ttl')->defaultNull()->end()
139
                                    ->scalarNode('respect_cache_headers')->defaultTrue()->end()
140
                                ->end()
141
                            ->end()
142
                        ->end()
143
                    ->end() // End cache plugin
144
145
                    ->arrayNode('cookie')
146
                    ->canBeEnabled()
147
                        ->children()
148
                            ->scalarNode('cookie_jar')->isRequired()->cannotBeEmpty()->end()
149
                        ->end()
150
                    ->end() // End cookie plugin
151
152
                    ->arrayNode('decoder')
153
                    ->canBeDisabled()
154
                    ->addDefaultsIfNotSet()
155
                        ->children()
156
                            ->scalarNode('use_content_encoding')->defaultTrue()->end()
157
                        ->end()
158
                    ->end() // End decoder plugin
159
160
                    ->arrayNode('history')
161
                    ->canBeEnabled()
162
                        ->children()
163
                            ->scalarNode('journal')->isRequired()->cannotBeEmpty()->end()
164
                        ->end()
165
                    ->end() // End history plugin
166
167
                    ->arrayNode('logger')
168
                    ->canBeEnabled()
169
                        ->children()
170
                            ->scalarNode('logger')->isRequired()->cannotBeEmpty()->end()
171
                            ->scalarNode('formatter')->defaultNull()->end()
172
                        ->end()
173
                    ->end() // End logger plugin
174
175
                    ->arrayNode('redirect')
176
                    ->canBeDisabled()
177
                    ->addDefaultsIfNotSet()
178
                        ->children()
179
                            ->scalarNode('preserve_header')->defaultTrue()->end()
180
                            ->scalarNode('use_default_for_multiple')->defaultTrue()->end()
181
                        ->end()
182
                    ->end() // End redirect plugin
183
184
                    ->arrayNode('retry')
185
                    ->canBeDisabled()
186
                    ->addDefaultsIfNotSet()
187
                        ->children()
188
                            ->scalarNode('retry')->defaultValue(1)->end()
189
                        ->end()
190
                    ->end() // End retry plugin
191
192
                    ->arrayNode('stopwatch')
193
                    ->canBeEnabled()
194
                        ->children()
195
                            ->scalarNode('stopwatch')->isRequired()->cannotBeEmpty()->end()
196
                        ->end()
197
                    ->end() // End stopwatch plugin
198
199
                ->end()
200
            ->end()
201
        ->end();
202
    }
203
}
204