Completed
Push — master ( e41328...feabfc )
by
unknown
15:35 queued 43s
created

NoxlogicRateLimitExtension::loadServices()   D

Complexity

Conditions 16
Paths 162

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 57.0316

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 96
ccs 37
cts 81
cp 0.4568
rs 4.1272
cc 16
nc 162
nop 2
crap 57.0316

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 3
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 3
        $configuration = new Configuration();
24 3
        $config = $this->processConfiguration($configuration, $configs);
25 3
        $this->loadServices($container, $config);
26
27 3
    }
28
29 3
    private function loadServices(ContainerBuilder $container, array $config)
30
    {
31 3
        $container->setParameter('noxlogic_rate_limit.enabled', $config['enabled']);
32
33 3
        $container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']);
34 3
        $container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']);
35 3
        $container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']);
36
37 3
        $container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']);
38 3
        $container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']);
39 3
        $container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']);
40 3
        $container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']);
41
42 3
        $container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']);
43
44 3
        switch ($config['storage_engine']) {
45 3
            case 'memcache':
46
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache');
47
                break;
48 3
            case 'redis':
49 3
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis');
50 3
                break;
51
            case 'doctrine':
52
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache');
53
                break;
54
            case 'php_redis';
55
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\PhpRedis');
56
                break;
57
            case 'simple_cache':
58
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\SimpleCache');
59
                break;
60 3
        }
61
62 3
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
63 3
        $loader->load('services.xml');
64
65 3
        switch ($config['storage_engine']) {
66 3
            case 'memcache':
67
                if (isset($config['memcache_client'])) {
68
                    $service = 'memcache.' . $config['memcache_client'];
69
                } else {
70
                    $service = $config['memcache_service'];
71
                }
72
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
73
                    0,
74
                    new Reference($service)
75
                );
76
                break;
77 3
            case 'redis':
78 3
                if (isset($config['redis_client'])) {
79 3
                    $service = 'snc_redis.' . $config['redis_client'];
80 3
                } else {
81
                    $service = $config['redis_service'];
82
                }
83 3
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
84 3
                    0,
85 3
                    new Reference($service)
86 3
                );
87 3
                break;
88
            case 'doctrine':
89
                if (isset($config['doctrine_provider'])) {
90
                    $service = 'doctrine_cache.providers.' . $config['doctrine_provider'];
91
                } else {
92
                    $service = $config['doctrine_service'];
93
                }
94
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
95
                    0,
96
                    new Reference($service)
97
                );
98
                break;
99
            case 'php_redis':
100
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
101
                    0,
102
                    new Reference($config['php_redis_service'])
103
                );
104
                break;
105
            case 'simple_cache':
106
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
107
                    0,
108
                    new Reference($config['simple_cache_service'])
109
                );
110 3
        }
111
112 3
        if ($config['fos_oauth_key_listener']) {
113
            // Set the SecurityContext for Symfony < 2.6
114
            // Replace with xml when < 2.6 is dropped.
115 3
            if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
116 3
                $tokenStorageReference = new Reference('security.token_storage');
117 3
            } else {
118
                $tokenStorageReference = new Reference('security.context');
119
            }
120 3
            $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
121 3
        } else {
122
            $container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener');
123
        }
124 3
    }
125
}
126