Completed
Pull Request — master (#40)
by Monse
02:20
created

NoxlogicRateLimitExtension::loadServices()   C

Complexity

Conditions 10
Paths 50

Size

Total Lines 68
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 19.0096

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 68
ccs 32
cts 58
cp 0.5517
rs 6.0995
cc 10
eloc 51
nc 50
nop 2
crap 19.0096

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\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
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
26 3
        if ($config['enabled'] === true) {
27 2
            $this->loadServices($container, $config);
28 2
        }
29 3
    }
30
31 2
    private function loadServices(ContainerBuilder $container, array $config)
32
    {
33 2
        $container->setParameter('noxlogic_rate_limit.rate_response_exception', $config['rate_response_exception']);
34 2
        $container->setParameter('noxlogic_rate_limit.rate_response_code', $config['rate_response_code']);
35 2
        $container->setParameter('noxlogic_rate_limit.rate_response_message', $config['rate_response_message']);
36
37 2
        $container->setParameter('noxlogic_rate_limit.display_headers', $config['display_headers']);
38 2
        $container->setParameter('noxlogic_rate_limit.headers.limit.name', $config['headers']['limit']);
39 2
        $container->setParameter('noxlogic_rate_limit.headers.remaining.name', $config['headers']['remaining']);
40 2
        $container->setParameter('noxlogic_rate_limit.headers.reset.name', $config['headers']['reset']);
41
42 2
        $container->setParameter('noxlogic_rate_limit.path_limits', $config['path_limits']);
43
44 2
        switch ($config['storage_engine']) {
45 2
            case 'memcache':
46
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Memcache');
47
                break;
48 2
            case 'redis':
49 2
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Redis');
50 2
                break;
51
            case 'doctrine':
52
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\DoctrineCache');
53
                break;
54
            case 'database':
55
                $container->setParameter('noxlogic_rate_limit.storage.class', 'Noxlogic\RateLimitBundle\Service\Storage\Database');
56
                break;
57 2
        }
58
59 2
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
60 2
        $loader->load('services.xml');
61
62 2
        switch ($config['storage_engine']) {
63 2
            case 'memcache':
64
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
65
                    0,
66
                    new Reference('memcache.' . $config['memcache_client'])
67
                );
68
                break;
69 2
            case 'redis':
70 2
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
71 2
                    0,
72 2
                    new Reference('snc_redis.' . $config['redis_client'])
73 2
                );
74 2
                break;
75
            case 'doctrine':
76
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
77
                    0,
78
                    new Reference('doctrine_cache.providers.' . $config['doctrine_provider'])
79
                );
80
                break;
81
            case 'database':
82
                $container->getDefinition('noxlogic_rate_limit.storage')->replaceArgument(
83
                    0,
84
                    new Reference('database.handler.pdo')
85
                );
86
                break;
87 2
        }
88
89
        // Set the SecurityContext for Symfony < 2.6
90
        // Replace with xml when < 2.6 is dropped.
91 2
        if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
92 2
            $tokenStorageReference = new Reference('security.token_storage');
93 2
        } else {
94
            $tokenStorageReference = new Reference('security.context');
95
        }
96 2
        $container->getDefinition('noxlogic_rate_limit.rate_limit_service')->replaceArgument(0, $tokenStorageReference);
97 2
        $container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
98 2
    }
99
}
100