Completed
Push — master ( 8dc349...5f2d53 )
by A.
02:44
created

Configuration::appendUrlConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 10
nc 1
nop 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
            ->arrayNode('enabled_second_factors')
69
                ->isRequired()
70
                ->prototype('scalar')
71
                    ->validate()
72
                        ->ifTrue(
73
                            function ($type) {
74
                                try {
75
                                    new SecondFactorType($type);
76
                                } catch (InvalidArgumentException $e) {
77
                                    return true;
78
                                } catch (DomainException $e) {
79
                                    return true;
80
                                }
81
                            }
82
                        )
83
                        ->thenInvalid(
84
                            'Enabled second factor type "%s" is not one of the valid types. See SecondFactorType'
85
                        )
86
                    ->end()
87
                ->end()
88
            ->end()
89
        ->end();
90
    }
91
92
    /**
93
     * @param NodeBuilder $childNodes
94
     */
95
    private function appendSessionConfiguration(NodeBuilder $childNodes)
96
    {
97
        $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...
98
            ->arrayNode('session_lifetimes')
99
                ->isRequired()
100
                ->children()
101
                    ->integerNode('max_absolute_lifetime')
102
                        ->isRequired()
103
                        ->defaultValue(3600)
104
                        ->info('The maximum lifetime of a session regardless of interaction by the user, in seconds.')
105
                        ->example('3600 -> 1 hour * 60 minutes * 60 seconds')
106
                        ->validate()
107
                            ->ifTrue(
108
                                function ($lifetime) {
109
                                    return !is_int($lifetime);
110
                                }
111
                            )
112
                            ->thenInvalid('max_absolute_lifetime must be an integer')
113
                        ->end()
114
                    ->end()
115
                    ->integerNode('max_relative_lifetime')
116
                        ->isRequired()
117
                        ->defaultValue(600)
118
                        ->info(
119
                            'The maximum relative lifetime of a session; the maximum allowed time between two '
120
                            . 'interactions by the user'
121
                        )
122
                        ->example('600 -> 10 minutes * 60 seconds')
123
                        ->validate()
124
                            ->ifTrue(
125
                                function ($lifetime) {
126
                                    return !is_int($lifetime);
127
                                }
128
                            )
129
                            ->thenInvalid('max_relative_lifetime must be an integer')
130
                        ->end()
131
                    ->end()
132
                ->end()
133
            ->end();
134
    }
135
136
    private function appendUrlConfiguration(NodeBuilder $childNodes)
137
    {
138
        $childNodes
139
            ->scalarNode('self_service_url')
140
                ->info('The URL of Self Service, where a user can register and revoke second factors')
141
                ->validate()
142
                    ->ifTrue(
143
                        function ($url) {
144
                            return filter_var($url, FILTER_VALIDATE_URL) === false;
145
                        }
146
                    )
147
                    ->thenInvalid('self_service_url must be a valid url')
148
            ->end();
149
    }
150
}
151