Completed
Push — master ( 185c3a...4e19f9 )
by Indra
01:19
created

Configuration::getConfigTreeBuilder()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 105
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 88
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 105
ccs 88
cts 88
cp 1
rs 6.4589
c 0
b 0
f 0
cc 7
eloc 89
nc 1
nop 0
crap 7

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
/*
4
 * This file is part of the ApiRateLimitBundle
5
 *
6
 * (c) Indra Gunawan <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Indragunawan\ApiRateLimitBundle\DependencyInjection;
13
14
use Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * The configuration of the bundle.
21
 *
22
 * @author Indra Gunawan <[email protected]>
23
 */
24
final class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 12
    public function getConfigTreeBuilder()
30
    {
31 12
        $treeBuilder = new TreeBuilder();
32 12
        $rootNode = $treeBuilder->root('indragunawan_api_rate_limit');
33
34
        $rootNode
35 12
            ->children()
36 12
                ->booleanNode('enabled')->defaultTrue()->end()
37 12
                ->scalarNode('storage')->defaultNull()->cannotBeEmpty()->end()
38 12
                ->scalarNode('cache')->defaultNull()->cannotBeEmpty()->end()
39 12
                ->arrayNode('header')
40 12
                    ->addDefaultsIfNotSet()
41 12
                    ->children()
42 12
                        ->booleanNode('display')->defaultTrue()->end()
43 12
                        ->arrayNode('names')
44 12
                            ->addDefaultsIfNotSet()
45 12
                            ->children()
46 12
                                ->scalarNode('limit')->cannotBeEmpty()->defaultValue('X-RateLimit-Limit')->end()
47 12
                                ->scalarNode('remaining')->cannotBeEmpty()->defaultValue('X-RateLimit-Remaining')->end()
48 12
                                ->scalarNode('reset')->cannotBeEmpty()->defaultValue('X-RateLimit-Reset')->end()
49 12
                            ->end()
50 12
                        ->end()
51 12
                    ->end()
52 12
                ->end()
53 12
                ->arrayNode('throttle')
54 12
                    ->beforeNormalization()
55
                        ->ifTrue(function ($v) { return is_array($v) && (isset($v['limit']) || isset($v['period'])); })
56
                        ->then(function ($v) {
57 1
                            $v['default'] = [];
58 1
                            if (isset($v['limit'])) {
59 1
                                @trigger_error('The indragunawan_api_rate_limit.throttle.limit configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.limit configuration key instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60
61 1
                                $v['default']['limit'] = $v['limit'];
62
                            }
63
64 1
                            if (isset($v['period'])) {
65 1
                                @trigger_error('The indragunawan_api_rate_limit.throttle.period configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.period configuration key instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
67 1
                                $v['default']['period'] = $v['period'];
68
                            }
69
70 1
                            return $v;
71 12
                        })
72 12
                    ->end()
73 12
                    ->addDefaultsIfNotSet()
74 12
                    ->children()
75 12
                        ->integerNode('limit')->min(1)->defaultValue(60)->end()
76 12
                        ->integerNode('period')->min(1)->defaultValue(60)->end()
77 12
                        ->arrayNode('default')
78 12
                            ->addDefaultsIfNotSet()
79 12
                            ->children()
80 12
                                ->integerNode('limit')->min(1)->defaultValue(60)->end()
81 12
                                ->integerNode('period')->min(1)->defaultValue(60)->end()
82 12
                            ->end()
83 12
                        ->end()
84 12
                        ->arrayNode('roles')
85 12
                            ->useAttributeAsKey('name')
86 12
                            ->prototype('array')
87 12
                                ->children()
88 12
                                    ->integerNode('limit')->isRequired()->min(1)->end()
89 12
                                    ->integerNode('period')->isRequired()->min(1)->end()
90 12
                                ->end()
91 12
                            ->end()
92 12
                        ->end()
93 12
                        ->enumNode('sort')
94 12
                            ->values(['first-match', 'rate-limit-asc', 'rate-limit-desc'])
95 12
                            ->defaultValue('rate-limit-desc')
96 12
                        ->end()
97 12
                    ->end()
98 12
                ->end()
99 12
                ->arrayNode('exception')
100 12
                    ->addDefaultsIfNotSet()
101 12
                    ->children()
102 12
                        ->integerNode('status_code')
103 12
                            ->defaultValue(Response::HTTP_TOO_MANY_REQUESTS)
104 12
                            ->validate()
105 12
                            ->ifNotInArray(array_keys(Response::$statusTexts))
106 12
                                ->thenInvalid('Invalid status code "%s"')
107 12
                            ->end()
108 12
                        ->end()
109 12
                        ->scalarNode('message')->cannotBeEmpty()->defaultValue('API rate limit exceeded for %s.')->end()
110 12
                        ->scalarNode('custom_exception')
111 12
                            ->cannotBeEmpty()
112 12
                            ->defaultNull()
113 12
                            ->validate()
114 12
                            ->ifTrue(function ($v) {
115 3
                                if (!class_exists($v)) {
116 1
                                    return true;
117
                                }
118
119 2
                                if (!is_subclass_of($v, RateLimitExceededException::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Indragunawan\ApiRateLim...xceededException::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
120 1
                                    return true;
121
                                }
122
123 1
                                return false;
124 12
                            })
125 12
                                ->thenInvalid('The class %s does not exist or not extend "Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException" class.')
126 12
                            ->end()
127 12
                        ->end()
128 12
                    ->end()
129 12
                ->end()
130 12
            ->end();
131
132 12
        return $treeBuilder;
133
    }
134
}
135