Completed
Push — develop ( 94d49a...7ffe43 )
by A.
11s
created

Configuration   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 124
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 13 1
A appendLoaConfiguration() 0 14 1
B appendSecondFactorTypesConfiguration() 0 27 3
B appendSessionConfiguration() 0 40 1
A appendUrlConfiguration() 0 14 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupRa\RaBundle\DependencyInjection;
20
21
use Surfnet\StepupBundle\Exception\DomainException;
22
use Surfnet\StepupBundle\Exception\InvalidArgumentException;
23
use Surfnet\StepupBundle\Value\SecondFactorType;
24
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
25
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
28
class Configuration implements ConfigurationInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getConfigTreeBuilder()
34
    {
35
        $treeBuilder = new TreeBuilder();
36
        $rootNode = $treeBuilder->root('surfnet_stepup_ra_ra');
37
38
        $childNodes = $rootNode->children();
39
        $this->appendLoaConfiguration($childNodes);
40
        $this->appendSecondFactorTypesConfiguration($childNodes);
41
        $this->appendSessionConfiguration($childNodes);
42
        $this->appendUrlConfiguration($childNodes);
43
44
        return $treeBuilder;
45
    }
46
47
    private function appendLoaConfiguration(NodeBuilder $childNodes)
48
    {
49
        $childNodes
50
            ->scalarNode('required_loa')
51
                ->info('The required LOA to be able to log in, should match the loa defined at the gateway')
52
                ->isRequired()
53
                    ->validate()
54
                        ->ifTrue(function ($value) {
55
                            return !is_string($value);
56
                        })
57
                        ->thenInvalid('the required loa must be a string')
58
                    ->end()
59
            ->end();
60
    }
61
62
    /**
63
     * @param NodeBuilder $childNodes
64
     */
65
    private function appendSecondFactorTypesConfiguration(NodeBuilder $childNodes)
66
    {
67
        $childNodes
68
69
            ->arrayNode('enabled_second_factors')
70
                ->isRequired()
71
                ->prototype('scalar')
72
                    ->validate()
73
                        ->ifTrue(
74
                            function ($type) {
75
                                try {
76
                                    new SecondFactorType($type);
77
                                } catch (InvalidArgumentException $e) {
78
                                    return true;
79
                                } catch (DomainException $e) {
80
                                    return true;
81
                                }
82
                            }
83
                        )
84
                        ->thenInvalid(
85
                            'Enabled second factor type "%s" is not one of the valid types. See SecondFactorType'
86
                        )
87
                    ->end()
88
                ->end()
89
            ->end()
90
        ->end();
91
    }
92
93
    /**
94
     * @param NodeBuilder $childNodes
95
     */
96
    private function appendSessionConfiguration(NodeBuilder $childNodes)
97
    {
98
        $childNodes
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
99
            ->arrayNode('session_lifetimes')
100
                ->isRequired()
101
                ->children()
102
                    ->integerNode('max_absolute_lifetime')
103
                        ->isRequired()
104
                        ->defaultValue(3600)
105
                        ->info('The maximum lifetime of a session regardless of interaction by the user, in seconds.')
106
                        ->example('3600 -> 1 hour * 60 minutes * 60 seconds')
107
                        ->validate()
108
                            ->ifTrue(
109
                                function ($lifetime) {
110
                                    return !is_int($lifetime);
111
                                }
112
                            )
113
                            ->thenInvalid('max_absolute_lifetime must be an integer')
114
                        ->end()
115
                    ->end()
116
                    ->integerNode('max_relative_lifetime')
117
                        ->isRequired()
118
                        ->defaultValue(600)
119
                        ->info(
120
                            'The maximum relative lifetime of a session; the maximum allowed time between two '
121
                            . 'interactions by the user'
122
                        )
123
                        ->example('600 -> 10 minutes * 60 seconds')
124
                        ->validate()
125
                            ->ifTrue(
126
                                function ($lifetime) {
127
                                    return !is_int($lifetime);
128
                                }
129
                            )
130
                            ->thenInvalid('max_relative_lifetime must be an integer')
131
                        ->end()
132
                    ->end()
133
                ->end()
134
            ->end();
135
    }
136
137
    private function appendUrlConfiguration(NodeBuilder $childNodes)
138
    {
139
        $childNodes
140
            ->scalarNode('self_service_url')
141
                ->info('The URL of Self Service, where a user can register and revoke second factors')
142
                ->validate()
143
                    ->ifTrue(
144
                        function ($url) {
145
                            return filter_var($url, FILTER_VALIDATE_URL) === false;
146
                        }
147
                    )
148
                    ->thenInvalid('self_service_url must be a valid url')
149
            ->end();
150
    }
151
}
152