Completed
Push — master ( a748b0...bc4a85 )
by
unknown
05:01 queued 03:35
created

NoxlogicRateLimitExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 34.29 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 58.57%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 36
loc 105
ccs 41
cts 70
cp 0.5857
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 7 1
C loadServices() 36 91 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_client'])) {
63 3
                    $service = 'snc_redis.' . $config['redis_client'];
64
                } else {
65
                    $service = $config['redis_service'];
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
            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 5
        if ($config['fos_oauth_key_listener']) {
108
            // Set the SecurityContext for Symfony < 2.6
109
            // Replace with xml when < 2.6 is dropped.
110 5
            if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
111 5
                $tokenStorageReference = new Reference('security.token_storage');
112
            } else {
113
                $tokenStorageReference = new Reference('security.context');
114
            }
115 5
            $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
116
        } else {
117
            $container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener');
118
        }
119 5
    }
120
}
121