Completed
Pull Request — develop (#11)
by Quentin
07:46 queued 04:57
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 88
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 88
ccs 0
cts 83
cp 0
rs 8.6012
cc 3
eloc 77
nc 1
nop 0
crap 12

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 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
                ->arrayNode('access_token')
40
                    ->addDefaultsIfNotSet()
41
                    ->children()
42
                        ->integerNode('ttl')
43
                            ->defaultValue(AccessTokenInterface::DEFAULT_TTL)
44
                        ->end()
45
                        ->scalarNode('class')
46
                            ->cannotBeEmpty()
47
                            ->defaultValue(AccessToken::class)
48
                            ->validate()
49
                                ->ifTrue(function ($accessTokenClass) {
50
                                    return !(
51
                                        class_exists($accessTokenClass, true)
52
                                        && (new \ReflectionClass($accessTokenClass))
53
                                            ->implementsInterface(AccessTokenInterface::class)
54
                                    );
55
                                })
56
                                ->thenInvalid('Provided access_token configuration has to be a valid class which implements Majora\Component\OAuth\Model\AccessTokenInterface.')
57
                            ->end()
58
                        ->end()
59
                    ->end()
60
                ->end()
61
62
                ->arrayNode('refresh_token')
63
                    ->addDefaultsIfNotSet()
64
                    ->children()
65
                        ->integerNode('ttl')
66
                            ->defaultValue(RefreshTokenInterface::DEFAULT_TTL)
67
                        ->end()
68
                        ->scalarNode('class')
69
                            ->cannotBeEmpty()
70
                            ->defaultValue(RefreshToken::class)
71
                            ->validate()
72
                                ->ifTrue(function ($refreshTokenClass) {
73
                                    return !(
74
                                        class_exists($refreshTokenClass, true)
75
                                        && (new \ReflectionClass($refreshTokenClass))
76
                                        ->implementsInterface(RefreshTokenInterface::class)
77
                                    );
78
                                })
79
                                ->thenInvalid('Provided refresh_token configuration has to be a valid class which implements Majora\Component\OAuth\Model\RefreshTokenInterface.')
80
                            ->end()
81
                        ->end()
82
                        ->scalarNode('loader')
83
                            ->isRequired()
84
                            ->cannotBeEmpty()
85
                        ->end()
86
                    ->end()
87
                ->end()
88
89
                ->arrayNode('application')
90
                    ->children()
91
                        ->scalarNode('loader')
92
                            ->isRequired()
93
                            ->cannotBeEmpty()
94
                        ->end()
95
                    ->end()
96
                ->end()
97
                ->arrayNode('account')
98
                    ->children()
99
                        ->scalarNode('loader')
100
                            ->isRequired()
101
                            ->cannotBeEmpty()
102
                        ->end()
103
                    ->end()
104
                ->end()
105
            ->end()
106
        ;
107
108
        return $treeBuilder;
109
    }
110
}
111