BgyOAuth2ServerExtension::load()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 43
rs 8.8571
cc 3
eloc 28
nc 2
nop 2
1
<?php
2
3
namespace Bgy\OAuth2ServerBundle\DependencyInjection;
4
5
use Bgy\OAuth2\AuthorizationServerConfiguration;
6
use Symfony\Component\DependencyInjection\Alias;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Reference;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
use Symfony\Component\DependencyInjection\Loader;
13
14
/**
15
 * This is the class that loads and manages your bundle configuration
16
 *
17
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
18
 */
19
class BgyOAuth2ServerExtension extends Extension
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function load(array $configs, ContainerBuilder $container)
25
    {
26
        $configuration = new Configuration();
27
        $config = $this->processConfiguration($configuration, $configs);
28
29
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
30
        $loader->load('services.xml');
31
32
        $authorizationServerConfig = $config['authorization_server'];
33
34
        $container->setAlias(
35
            'bgy_oauth2_server.authorization_server.storage.access_token',
36
            new Alias($authorizationServerConfig['storage']['access_token'], true)
37
        );
38
        $container->setAlias(
39
            'bgy_oauth2_server.authorization_server.storage.client',
40
            new Alias($authorizationServerConfig['storage']['client'], true)
41
        );
42
        $container->setAlias(
43
            'bgy_oauth2_server.authorization_server.storage.refresh_token',
44
            new Alias($authorizationServerConfig['storage']['refresh_token'], true)
45
        );
46
47
        $container->setAlias(
48
            'bgy_oauth2_server.authorization_server.token_generator',
49
            new Alias($authorizationServerConfig['token_generator'] ?: 'bgy_oauth2_server.authorization_server.token_generator.php7_csprng', true)
50
        );
51
52
        $grantTypesServices = [];
53
54
        foreach ($authorizationServerConfig['grant_types'] as $serviceId) {
55
            $grantTypesServices[] = new Reference($serviceId);
56
        }
57
58
        $container->getDefinition('bgy_oauth2_server.authorization_server')->replaceArgument(4, $grantTypesServices);
59
        $container->getDefinition('bgy_oauth2_server.authorization_server.configuration')->replaceArgument(1, [
60
            'always_require_a_client'         => $authorizationServerConfig['always_require_a_client'],
61
            'always_generate_a_refresh_token' => $authorizationServerConfig['always_generate_a_refresh_token'],
62
            'access_token_ttl'                => $authorizationServerConfig['access_token_ttl'],
63
            'refresh_token_ttl'               => $authorizationServerConfig['refresh_token_ttl'],
64
            'revoke_refresh_token_when_used'               => $authorizationServerConfig['revoke_refresh_token_when_used'],
65
        ]);
66
    }
67
}
68