Passed
Pull Request — master (#14)
by Indra
01:50
created

Configuration::getConfigTreeBuilder()   C

Complexity

Conditions 8
Paths 2

Size

Total Lines 105
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 88
CRAP Score 8

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 89
c 2
b 1
f 0
dl 0
loc 105
ccs 88
cts 88
cp 1
rs 6.9955
cc 8
nc 2
nop 0
crap 8

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 11
    public function getConfigTreeBuilder()
30
    {
31 11
        $treeBuilder = new TreeBuilder('indragunawan_api_rate_limit');
32 11
        $rootNode = method_exists(TreeBuilder::class, 'getRootNode')
33 11
            ? $treeBuilder->getRootNode()
34 11
            : $treeBuilder->root('indragunawan_api_rate_limit');
0 ignored issues
show
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
            : $treeBuilder->/** @scrutinizer ignore-call */ root('indragunawan_api_rate_limit');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
        $rootNode
37 11
            ->children()
38 11
                ->booleanNode('enabled')->defaultTrue()->end()
39 11
                ->scalarNode('cache')->defaultNull()->cannotBeEmpty()->end()
40 11
                ->arrayNode('header')
41 11
                    ->addDefaultsIfNotSet()
42 11
                    ->children()
43 11
                        ->booleanNode('display')->defaultTrue()->end()
44 11
                        ->arrayNode('names')
45 11
                            ->addDefaultsIfNotSet()
46 11
                            ->children()
47 11
                                ->scalarNode('limit')->cannotBeEmpty()->defaultValue('X-RateLimit-Limit')->end()
48 11
                                ->scalarNode('remaining')->cannotBeEmpty()->defaultValue('X-RateLimit-Remaining')->end()
49 11
                                ->scalarNode('reset')->cannotBeEmpty()->defaultValue('X-RateLimit-Reset')->end()
50 11
                            ->end()
51 11
                        ->end()
52 11
                    ->end()
53 11
                ->end()
54 11
                ->arrayNode('throttle')
55 11
                    ->beforeNormalization()
56
                        ->ifTrue(function ($v) { return is_array($v) && (isset($v['limit']) || isset($v['period'])); })
57
                        ->then(function ($v) {
58 1
                            $v['default'] = [];
59 1
                            if (isset($v['limit'])) {
60 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);
61
62 1
                                $v['default']['limit'] = $v['limit'];
63
                            }
64
65 1
                            if (isset($v['period'])) {
66 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);
67
68 1
                                $v['default']['period'] = $v['period'];
69
                            }
70
71 1
                            return $v;
72 11
                        })
73 11
                    ->end()
74 11
                    ->addDefaultsIfNotSet()
75 11
                    ->children()
76 11
                        ->integerNode('limit')->min(1)->defaultValue(60)->end()
77 11
                        ->integerNode('period')->min(1)->defaultValue(60)->end()
78 11
                        ->arrayNode('default')
79 11
                            ->addDefaultsIfNotSet()
80 11
                            ->children()
81 11
                                ->integerNode('limit')->min(1)->defaultValue(60)->end()
82 11
                                ->integerNode('period')->min(1)->defaultValue(60)->end()
83 11
                            ->end()
84 11
                        ->end()
85 11
                        ->arrayNode('roles')
86 11
                            ->useAttributeAsKey('name')
87 11
                            ->prototype('array')
88 11
                                ->children()
89 11
                                    ->integerNode('limit')->isRequired()->min(1)->end()
90 11
                                    ->integerNode('period')->isRequired()->min(1)->end()
91 11
                                ->end()
92 11
                            ->end()
93 11
                        ->end()
94 11
                        ->enumNode('sort')
95 11
                            ->values(['first-match', 'rate-limit-asc', 'rate-limit-desc'])
96 11
                            ->defaultValue('rate-limit-desc')
97 11
                        ->end()
98 11
                    ->end()
99 11
                ->end()
100 11
                ->arrayNode('exception')
101 11
                    ->addDefaultsIfNotSet()
102 11
                    ->children()
103 11
                        ->integerNode('status_code')
104 11
                            ->defaultValue(Response::HTTP_TOO_MANY_REQUESTS)
105 11
                            ->validate()
106 11
                            ->ifNotInArray(array_keys(Response::$statusTexts))
107 11
                                ->thenInvalid('Invalid status code "%s"')
108 11
                            ->end()
109 11
                        ->end()
110 11
                        ->scalarNode('message')->cannotBeEmpty()->defaultValue('API rate limit exceeded for %s.')->end()
111 11
                        ->scalarNode('custom_exception')
112 11
                            ->cannotBeEmpty()
113 11
                            ->defaultNull()
114 11
                            ->validate()
115
                            ->ifTrue(function ($v) {
116 3
                                if (!class_exists($v)) {
117 1
                                    return true;
118
                                }
119
120 2
                                if (!is_subclass_of($v, RateLimitExceededException::class)) {
121 1
                                    return true;
122
                                }
123
124 1
                                return false;
125 11
                            })
126 11
                                ->thenInvalid('The class %s does not exist or not extend "Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException" class.')
127 11
                            ->end()
128 11
                        ->end()
129 11
                    ->end()
130 11
                ->end()
131 11
            ->end();
132
133 11
        return $treeBuilder;
134
    }
135
}
136