Failed Conditions
Push — master ( 303035...e43aad )
by Florent
03:43
created

RefreshTokenSource::getNodeDefinition()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Component\Grant\RefreshToken;
15
16
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenGrantType;
17
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenIdGenerator;
18
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenRepository;
19
use OAuth2Framework\ServerBundle\Component\Component;
20
use OAuth2Framework\ServerBundle\Service\RandomRefreshTokenIdGenerator;
21
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25
26
class RefreshTokenSource implements Component
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function name(): string
32
    {
33
        return 'refresh_token';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function load(array $configs, ContainerBuilder $container)
40
    {
41
        if (!class_exists(RefreshTokenGrantType::class) || !$configs['grant']['refresh_token']['enabled']) {
42
            return;
43
        }
44
        $container->setParameter('oauth2_server.grant.refresh_token.lifetime', $configs['grant']['refresh_token']['lifetime']);
45
        $container->setAlias(RefreshTokenRepository::class, $configs['grant']['refresh_token']['repository']);
46
        $container->setAlias(RefreshTokenIdGenerator::class, $configs['grant']['refresh_token']['id_generator']);
47
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant'));
48
        $loader->load('refresh_token.php');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
55
    {
56
        if (!class_exists(RefreshTokenGrantType::class)) {
57
            return;
58
        }
59
        $node->children()
60
            ->arrayNode('refresh_token')
61
                ->canBeEnabled()
62
                ->children()
63
                    ->integerNode('lifetime')
64
                        ->defaultValue(60 * 60 * 24 * 7)
65
                        ->min(1)
66
                        ->info('The refresh token lifetime (in seconds)')
67
                    ->end()
68
                    ->scalarNode('repository')
69
                        ->isRequired()
70
                        ->info('The refresh token repository')
71
                    ->end()
72
                    ->scalarNode('id_generator')
73
                        ->info('The refresh token ID generator service')
74
                        ->defaultValue(RandomRefreshTokenIdGenerator::class)
75
                    ->end()
76
                ->end()
77
            ->end()
78
        ->end();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function build(ContainerBuilder $container)
85
    {
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function prepend(ContainerBuilder $container, array $config): array
92
    {
93
        return [];
94
    }
95
}
96