MajoraOAuthServerExtension::getAlias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        // token generator
43
        $randomGeneratorDefinition = $container->getDefinition('majora.oauth.random_generator');
44
        $randomGeneratorDefinition->replaceArgument(0, $config['secret']);
45
46
        // server
47
        $serverDefinition = $container->getDefinition('majora.oauth.server');
48
        $serverDefinition->replaceArgument(4, array(
49
            'access_token_class' => $config['access_token']['class'],
50
            'access_token_ttl' => $config['access_token']['ttl'],
51
            'refresh_token_class' => $config['refresh_token']['class'],
52
            'refresh_token_ttl' => $config['refresh_token']['ttl'],
53
        ));
54
55
        // aliases generation
56
        foreach (array('access_token', 'refresh_token', 'application', 'account') as $entity) {
57
            foreach (array('loader', 'repository') as $serviceAlias) {
58
                foreach ($config[$entity][$serviceAlias] as $driver => $parameters) {
59
60
                    // given service id or build one from registered strategies
61
                    $serviceId = $driver == 'id' ?
62
                        $parameters :
63
                        sprintf('majora.oauth.%s.%s_%s', $entity, $driver, $serviceAlias)
64
                    ;
65
66
                    // publish given service
67
                    $container->setAlias(
68
                        sprintf('majora.oauth.%s.%s', $entity, $serviceAlias),
69
                        $serviceId
70
                    );
71
72
                    // register parameters under driver if given
73
                    //
74
                    // !! implements here a better strategy !!
75
                    //
76
                    if (is_array($parameters)) {
77
                        foreach ($parameters as $key => $value) {
78
                            $container->setParameter(
79
                                sprintf('majora.oauth.%s.%s_%s.%s', $entity, $driver, $serviceAlias, $key),
80
                                $value
81
                            );
82
                        }
83
                    }
84
                }
85
            }
86
        }
87
    }
88
}
89