Completed
Push — master ( 41e333...e6c76f )
by Joshua
17s queued 11s
created

NoxlogicRateLimitExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 47.17 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 54.05%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 7
dl 50
loc 106
ccs 40
cts 74
cp 0.5405
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 7 1
C loadServices() 50 92 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 5
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 5
        $configuration = new Configuration();
24 5
        $config = $this->processConfiguration($configuration, $configs);
25 5
        $this->loadServices($container, $config);
26
27 5
    }
28
29 5
    private function loadServices(ContainerBuilder $container, array $config)
30
    {
31 5
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
32 5
        $loader->load('services.xml');
33
34 5
        $container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']);
35
36 5
        $container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']);
37 5
        $container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']);
38 5
        $container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']);
39
40 5
        $container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']);
41 5
        $container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']);
42 5
        $container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']);
43 5
        $container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']);
44
45 5
        $container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']);
46
47 5
        switch ($config['storage_engine']) {
48 5 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 5 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 3
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis');
62 3
                if (isset($config['redis_service'])) {
63
                    $service = $config['redis_service'];
64
                } else {
65 3
                    $service = 'snc_redis.' . $config['redis_client'];
66
                }
67 3
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
68 3
                    0,
69 3
                    new Reference($service)
70
                );
71 3
                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 View Code Duplication
            case 'php_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...
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 View Code Duplication
            case 'php_redis_cluster':
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...
92
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedisCluster');
93
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
94
                    0,
95
                    new Reference($config['php_redis_service'])
96
                );
97
                break;
98
            case 'simple_cache':
99
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache');
100
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
101
                    0,
102
                    new Reference($config['simple_cache_service'])
103
                );
104
                break;
105
            case 'cache':
106
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PsrCache');
107
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
108
                    0,
109
                    new Reference($config['cache_service'])
110
                );
111
                break;
112
        }
113
114 5
        if ($config['fos_oauth_key_listener']) {
115 5
            $tokenStorageReference = new Reference('security.token_storage');
116 5
            $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
117
        } else {
118
            $container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener');
119
        }
120 5
    }
121
}
122