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

NoxlogicRateLimitExtension::loadServices()   C

Complexity

Conditions 11
Paths 20

Size

Total Lines 85

Duplication

Lines 36
Ratio 42.35 %

Code Coverage

Tests 35
CRAP Score 21.6195

Importance

Changes 0
Metric Value
dl 36
loc 85
ccs 35
cts 63
cp 0.5556
rs 6.1806
c 0
b 0
f 0
cc 11
nc 20
nop 2
crap 21.6195

How to fix   Long Method    Complexity   

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\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
11
/**
12
 * This is the class that loads and manages your bundle configuration
13
 *
14
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
15
 */
16
class NoxlogicRateLimitExtension extends Extension
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21 4
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 4
        $configuration = new Configuration();
24 4
        $config = $this->processConfiguration($configuration, $configs);
25 4
        $this->loadServices($container, $config);
26
27 4
    }
28
29 4
    private function loadServices(ContainerBuilder $container, array $config)
30
    {
31 4
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
32 4
        $loader->load('services.xml');
33
34 4
        $container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']);
35
36 4
        $container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']);
37 4
        $container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']);
38 4
        $container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']);
39
40 4
        $container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']);
41 4
        $container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']);
42 4
        $container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']);
43 4
        $container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']);
44
45 4
        $container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']);
46
47 4
        switch ($config['storage_engine']) {
48 4 View Code Duplication
            case 'memcache':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache');
50
                if (isset($config['memcache_client'])) {
51
                    $service = 'memcache.' . $config['memcache_client'];
52
                } else {
53
                    $service = $config['memcache_service'];
54
                }
55
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
56
                    0,
57
                    new Reference($service)
58
                );
59
                break;
60 4 View Code Duplication
            case 'redis':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61 2
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis');
62 2
                if (isset($config['redis_client'])) {
63 2
                    $service = 'snc_redis.' . $config['redis_client'];
64
                } else {
65
                    $service = $config['redis_service'];
66
                }
67 2
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
68 2
                    0,
69 2
                    new Reference($service)
70
                );
71 2
                break;
72 2 View Code Duplication
            case 'doctrine':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73 2
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache');
74 2
                if (isset($config['doctrine_provider'])) {
75 1
                    $service = 'doctrine_cache.providers.' . $config['doctrine_provider'];
76
                } else {
77 1
                    $service = $config['doctrine_service'];
78
                }
79 2
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
80 2
                    0,
81 2
                    new Reference($service)
82
                );
83 2
                break;
84
            case 'php_redis':
85
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedis');
86
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
87
                    0,
88
                    new Reference($config['php_redis_service'])
89
                );
90
                break;
91
            case 'simple_cache':
92
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache');
93
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
94
                    0,
95
                    new Reference($config['simple_cache_service'])
96
                );
97
                break;
98
            case 'cache':
99
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PsrCache');
100
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
101
                    0,
102
                    new Reference($config['cache_service'])
103
                );
104
                break;
105
        }
106
107 4
        if ($config['fos_oauth_key_listener']) {
108 4
            $tokenStorageReference = new Reference('security.token_storage');
109 4
            $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
110
        } else {
111
            $container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener');
112
        }
113 4
    }
114
}
115