Completed
Pull Request — master (#37)
by Tobias
28:35 queued 28:35
created

Configuration::configurePlugins()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 89
rs 8.5731
cc 1
eloc 77
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
 * @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')->isRequired()->cannotBeEmpty()->end()
128
                        ->end()
129
                    ->end() // End authentication plugin
130
131
                    ->arrayNode('cache')
132
                    ->canBeEnabled()
133
                    ->addDefaultsIfNotSet()
134
                        ->children()
135
                            ->scalarNode('cache_pool')->isRequired()->cannotBeEmpty()->end()
136
                            ->scalarNode('stream_factory')->cannotBeEmpty()->defaultValue('httplug.stream_factory')->end()
137
                            ->arrayNode('config')
138
                                ->children()
139
                                    ->scalarNode('default_ttl')->defaultNull()->end()
140
                                    ->scalarNode('respect_cache_headers')->defaultTrue()->end()
141
                                ->end()
142
                            ->end()
143
                        ->end()
144
                    ->end() // End cache plugin
145
146
                    ->arrayNode('cookie')
147
                    ->canBeEnabled()
148
                        ->children()
149
                            ->scalarNode('cookie_jar')->isRequired()->cannotBeEmpty()->end()
150
                        ->end()
151
                    ->end() // End cookie plugin
152
153
                    ->arrayNode('decoder')
154
                    ->canBeDisabled()
155
                    ->addDefaultsIfNotSet()
156
                        ->children()
157
                            ->scalarNode('use_content_encoding')->defaultTrue()->end()
158
                        ->end()
159
                    ->end() // End decoder plugin
160
161
                    ->arrayNode('history')
162
                    ->canBeEnabled()
163
                        ->children()
164
                            ->scalarNode('journal')->isRequired()->cannotBeEmpty()->end()
165
                        ->end()
166
                    ->end() // End history plugin
167
168
                    ->arrayNode('logger')
169
                    ->canBeDisabled()
170
                    ->addDefaultsIfNotSet()
171
                        ->children()
172
                            ->scalarNode('logger')->defaultValue('logger')->cannotBeEmpty()->end()
173
                            ->scalarNode('formatter')->defaultNull()->end()
174
                        ->end()
175
                    ->end() // End logger plugin
176
177
                    ->arrayNode('redirect')
178
                    ->canBeDisabled()
179
                    ->addDefaultsIfNotSet()
180
                        ->children()
181
                            ->scalarNode('preserve_header')->defaultTrue()->end()
182
                            ->scalarNode('use_default_for_multiple')->defaultTrue()->end()
183
                        ->end()
184
                    ->end() // End redirect plugin
185
186
                    ->arrayNode('retry')
187
                    ->canBeDisabled()
188
                    ->addDefaultsIfNotSet()
189
                        ->children()
190
                            ->scalarNode('retry')->defaultValue(1)->end()
191
                        ->end()
192
                    ->end() // End retry plugin
193
194
                    ->arrayNode('stopwatch')
195
                    ->canBeDisabled()
196
                    ->addDefaultsIfNotSet()
197
                        ->children()
198
                            ->scalarNode('stopwatch')->defaultValue('debug.stopwatch')->cannotBeEmpty()->end()
199
                        ->end()
200
                    ->end() // End stopwatch plugin
201
202
                ->end()
203
            ->end()
204
        ->end();
205
    }
206
}
207