Completed
Push — nyx-refresh-token-orm-config ( 30c2e2 )
by Quentin
14:03
created

Configuration::createTokenNode()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 32
cp 0
rs 8.8571
cc 2
eloc 27
nc 1
nop 4
crap 6
1
<?php
2
3
namespace Majora\Bundle\OAuthServerBundle\DependencyInjection;
4
5
use Majora\Component\OAuth\Entity\AccessToken;
6
use Majora\Component\OAuth\Entity\RefreshToken;
7
use Majora\Component\OAuth\Model\AccessTokenInterface;
8
use Majora\Component\OAuth\Model\RefreshTokenInterface;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
12
/**
13
 * Majora OAuth server bundle semantical configuration class.
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     *
20
     * @see http://symfony.com/doc/current/components/config/definition.html
21
     */
22
    public function getConfigTreeBuilder()
23
    {
24
        $supportedDrivers = array('orm');
25
26
        $treeBuilder = new TreeBuilder();
27
        $treeBuilder->root('majora_oauth_server')
28
            ->children()
29
                ->scalarNode('db_driver')
30
                    ->validate()
31
                        ->ifNotInArray($supportedDrivers)
32
                        ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
33
                    ->end()
34
                ->end()
35
                ->scalarNode('secret')
36
                    ->isRequired()
37
                    ->cannotBeEmpty()
38
                ->end()
39
                ->append($this->createTokenNode(
40
                    'access_token',
41
                    AccessToken::class,
42
                    AccessTokenInterface::class,
43
                    AccessTokenInterface::DEFAULT_TTL
44
                ))
45
                ->append($this->createTokenNode(
46
                    'refresh_token',
47
                    RefreshToken::class,
48
                    RefreshTokenInterface::class,
49
                    RefreshTokenInterface::DEFAULT_TTL
50
                ))
51
                ->arrayNode('application')
52
                    ->children()
53
                        ->append($this->createDriverStrategyNode('loader'))
54
                        ->append($this->createDriverStrategyNode('repository'))
55
                    ->end()
56
                ->end()
57
                ->arrayNode('account')
58
                    ->children()
59
                        ->append($this->createDriverStrategyNode('loader'))
60
                        ->append($this->createDriverStrategyNode('repository'))
61
                    ->end()
62
                ->end()
63
            ->end()
64
        ;
65
66
        return $treeBuilder;
67
    }
68
69
    /**
70
     * Create a token configuration node.
71
     *
72
     * @param string $tokenName
73
     * @param string $defaultClass
74
     * @param string $tokenInterface
75
     * @param string $defaultTtl
76
     *
77
     * @return Node
78
     */
79
    private function createTokenNode($tokenName, $defaultClass, $tokenInterface, $defaultTtl)
80
    {
81
        $builder = new TreeBuilder();
82
        $node = $builder->root($tokenName);
83
        $node
84
            ->addDefaultsIfNotSet()
85
            ->children()
86
                ->integerNode('ttl')
87
                    ->defaultValue($defaultTtl)
88
                ->end()
89
                ->scalarNode('class')
90
                    ->cannotBeEmpty()
91
                    ->defaultValue($defaultClass)
92
                    ->validate()
93
                        ->ifTrue(function ($accessTokenClass) use ($tokenInterface) {
94
                            return !(
95
                                class_exists($accessTokenClass, true)
96
                                && (new \ReflectionClass($accessTokenClass))
97
                                    ->implementsInterface($tokenInterface)
98
                            );
99
                        })
100
                        ->thenInvalid(sprintf(
101
                            'Provided access_token configuration has to be a valid class which implements %s.',
102
                            $tokenInterface
103
                        ))
104
                    ->end()
105
                ->end()
106
                ->append($this->createDriverStrategyNode('loader'))
107
                ->append($this->createDriverStrategyNode('repository'))
108
            ->end()
109
        ;
110
111
        return $node;
112
    }
113
114
    private function createDriverStrategyNode($strategyName)
115
    {
116
        $builder = new TreeBuilder();
117
        $node = $builder->root($strategyName);
118
        $node
119
            ->addDefaultsIfNotSet()
120
            ->children()
121
                ->scalarNode('id')->end()
122
                ->arrayNode('orm')->end()
123
                ->scalarNode('none')->defaultValue(true)->end()
124
            ->end()
125
            ->validate()
126
                ->ifTrue(function ($data) {
127
                    var_dump($data); die;
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method createDriverStrategyNode() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
128
                })
129
                ->thenInvalid(sprintf(
130
                    'Provided access_token configuration has to be a valid class which implements %s.',
131
                    $tokenInterface
0 ignored issues
show
Bug introduced by
The variable $tokenInterface does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
132
                ))
133
            ->end()
134
        ->end()
135
        ;
136
137
        return $node;
138
    }
139
}
140