Completed
Push — master ( a281b4...b9e4e8 )
by Joshua
15s queued 11s
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 130

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 119
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 130
ccs 119
cts 120
cp 0.9917
rs 8
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4

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 Noxlogic\RateLimitBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8
9
/**
10
 * This is the class that validates and merges configuration from your app/config files
11
 *
12
 */
13
class Configuration implements ConfigurationInterface
14
{
15
    const HTTP_TOO_MANY_REQUESTS = 429;
16
17
    /**
18
     * {@inheritDoc}
19
     */
20 12
    public function getConfigTreeBuilder()
21
    {
22 12
        $treeBuilder = new TreeBuilder('noxlogic_rate_limit');
23
        // Keep compatibility with symfony/config < 4.2
24 12
        if (\method_exists($treeBuilder, 'getRootNode')) {
25 12
            $rootNode = $treeBuilder->getRootNode();
26
        } else {
27
            $rootNode = $treeBuilder->root('noxlogic_rate_limit');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
28
        }
29
        $rootNode
30 12
            ->canBeDisabled()
31 12
            ->children()
32 12
                ->enumNode('storage_engine')
33 12
                    ->values(array('redis','memcache','doctrine', 'php_redis', 'simple_cache', 'cache'))
34 12
                    ->defaultValue('redis')
35 12
                    ->info('The storage engine where all the rates will be stored')
36 12
                ->end()
37 12
                ->scalarNode('redis_client')
38 12
                    ->defaultValue('default_client')
39 12
                    ->info('The redis client to use for the redis storage engine')
40 12
                ->end()
41 12
                ->scalarNode('redis_service')
42 12
                    ->defaultNull()
43 12
                    ->info('The Redis service to use for the redis storage engine, should be instance of \\Predis\\Client')
44 12
                    ->example('project.predis')
45 12
                ->end()
46 12
                ->scalarNode('php_redis_service')
47 12
                    ->defaultNull()
48 12
                    ->info('Service id of a php redis, should be an instance of \\Redis')
49 12
                    ->example('project.redis')
50 12
                ->end()
51 12
                ->scalarNode('memcache_client')
52 12
                    ->defaultValue('default')
53 12
                    ->info('The memcache client to use for the memcache storage engine')
54 12
                ->end()
55 12
                ->scalarNode('memcache_service')
56 12
                    ->defaultNull()
57 12
                    ->info('The Memcached service to use for the memcache storage engine, should be instance of \\Memcached')
58 12
                    ->example('project.memcached')
59 12
                ->end()
60 12
                ->scalarNode('doctrine_provider')
61 12
                    ->defaultNull()
62 12
                    ->info('The Doctrine Cache provider to use for the doctrine storage engine')
63 12
                    ->example('my_apc_cache')
64 12
                ->end()
65 12
                ->scalarNode('doctrine_service')
66 12
                    ->defaultNull()
67 12
                    ->info('The Doctrine Cache service to use for the doctrine storage engine')
68 12
                    ->example('project.my_apc_cache')
69 12
                ->end()
70 12
                ->scalarNode('simple_cache_service')
71 12
                    ->defaultNull()
72 12
                    ->info('Service id of a simple cache, should be an instance of \\Psr\\SimpleCache\\CacheInterface')
73 12
                    ->example('project.cache')
74 12
                ->end()
75 12
                ->scalarNode('cache_service')
76 12
                    ->defaultNull()
77 12
                    ->info('Service id of a cache, should be an instance of \\Psr\\Cache\\CacheItemPoolInterface')
78 12
                    ->example('project.cache')
79 12
                ->end()
80 12
                ->integerNode('rate_response_code')
81 12
                    ->min(400)
82 12
                    ->max(499)
83 12
                    ->defaultValue(static::HTTP_TOO_MANY_REQUESTS)
84 12
                    ->info('The HTTP status code to return when a client hits the rate limit')
85 12
                ->end()
86 12
                ->scalarNode('rate_response_exception')
87 12
                    ->defaultNull()
88 12
                    ->info('Optional exception class that will be returned when a client hits the rate limit')
89 12
                    ->validate()
90
                        ->always(function ($item) {
91 3
                            if ($item && !is_subclass_of($item, '\Exception')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
92 1
                                throw new InvalidConfigurationException(sprintf("'%s' must inherit the \\Exception class", $item));
93
                            }
94 2
                            return $item;
95 12
                        })
96 12
                    ->end()
97 12
                ->end()
98 12
                ->scalarNode('rate_response_message')
99 12
                    ->defaultValue('You exceeded the rate limit')
100 12
                    ->info('The HTTP message to return when a client hits the rate limit')
101 12
                ->end()
102 12
                ->booleanNode('display_headers')
103 12
                    ->defaultTrue()
104 12
                    ->info('Should the ratelimit headers be automatically added to the response?')
105 12
                ->end()
106 12
                ->arrayNode('headers')
107 12
                    ->addDefaultsIfNotSet()
108 12
                    ->info('What are the different header names to add')
109 12
                    ->children()
110 12
                        ->scalarNode('limit')->defaultValue('X-RateLimit-Limit')->end()
111 12
                        ->scalarNode('remaining')->defaultValue('X-RateLimit-Remaining')->end()
112 12
                        ->scalarNode('reset')->defaultValue('X-RateLimit-Reset')->end()
113 12
                    ->end()
114 12
                ->end()
115 12
                ->arrayNode('path_limits')
116 12
                    ->defaultValue(array())
117 12
                    ->info('Rate limits for paths')
118 12
                    ->prototype('array')
119 12
                        ->children()
120 12
                            ->scalarNode('path')
121 12
                                ->isRequired()
122 12
                            ->end()
123 12
                            ->arrayNode('methods')
124 12
                                ->prototype('enum')
125 12
                                    ->values(array('*', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'))
126 12
                                ->end()
127 12
                                ->requiresAtLeastOneElement()
128 12
                                ->defaultValue(array('*'))
129 12
                            ->end()
130 12
                            ->integerNode('limit')
131 12
                                ->isRequired()
132 12
                                ->min(0)
133 12
                            ->end()
134 12
                            ->integerNode('period')
135 12
                                ->isRequired()
136 12
                                ->min(0)
137 12
                            ->end()
138 12
                        ->end()
139 12
                    ->end()
140 12
                ->end()
141 12
                ->booleanNode('fos_oauth_key_listener')
142 12
                    ->defaultTrue()
143 12
                    ->info('Enabled the FOS OAuthServerBundle listener')
144 12
                ->end()
145 12
            ->end()
146
        ;
147
148 12
        return $treeBuilder;
149
    }
150
}
151