Completed
Push — nyx-check-token ( 52cbdc )
by Quentin
04:03
created

MajoraOAuthServerExtension::load()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 63
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 63
rs 6.8825
cc 8
eloc 32
nc 10
nop 2

How to fix   Long Method   

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 Majora\Bundle\OAuthServerBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Extension\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
10
/**
11
 * This is the class that loads and manages your bundle configuration.
12
 *
13
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14
 */
15
class MajoraOAuthServerExtension extends Extension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getAlias()
21
    {
22
        return 'majora_oauth_server';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function load(array $configs, ContainerBuilder $container)
29
    {
30
        $config = $this->processConfiguration(new Configuration(), $configs);
31
32
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
33
        $loader->load('server.xml');
34
        $loader->load('extensions.xml');
35
        $loader->load('empty.xml');
36
        $loader->load('orm.xml');
37
38
        if (!$container->hasDefinition('majora.oauth.server')) {
39
            return;
40
        }
41
42
        // folder path storage : required for doctrine schema registery
43
        $container->setParameter('majora.oauth.bundle_dir', realpath(__DIR__.'/..'));
44
45
        // token generator
46
        $randomGeneratorDefinition = $container->getDefinition('majora.oauth.random_generator');
47
        $randomGeneratorDefinition->replaceArgument(0, $config['secret']);
48
49
        // server
50
        $serverDefinition = $container->getDefinition('majora.oauth.server');
51
        $serverDefinition->replaceArgument(4, array(
52
            'access_token_class' => $config['access_token']['class'],
53
            'access_token_ttl' => $config['access_token']['ttl'],
54
            'refresh_token_class' => $config['refresh_token']['class'],
55
            'refresh_token_ttl' => $config['refresh_token']['ttl'],
56
        ));
57
58
        // aliases generation
59
        foreach (array('access_token', 'refresh_token', 'application', 'account') as $entity) {
60
            foreach (array('loader', 'repository') as $serviceAlias) {
61
                foreach ($config[$entity][$serviceAlias] as $driver => $parameters) {
62
63
                    // given service id or build one from registered strategies
64
                    $serviceId = $driver == 'id' ?
65
                        $parameters :
66
                        sprintf('majora.oauth.%s.%s_%s', $entity, $driver, $serviceAlias)
67
                    ;
68
69
                    // publish given service
70
                    $container->setAlias(
71
                        sprintf('majora.oauth.%s.%s', $entity, $serviceAlias),
72
                        $serviceId
73
                    );
74
75
                    // register parameters under driver if given
76
                    //
77
                    // !! implements here a better strategy !!
78
                    //
79
                    if (is_array($parameters)) {
80
                        foreach ($parameters as $key => $value) {
81
                            $container->setParameter(
82
                                sprintf('majora.oauth.%s.%s_%s.%s', $entity, $driver, $serviceAlias, $key),
83
                                $value
84
                            );
85
                        }
86
                    }
87
                }
88
            }
89
        }
90
    }
91
}
92