Completed
Pull Request — develop (#107)
by Boy
25:48 queued 21:51
created

validateListOfNameIdPatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 5
nc 1
nop 3
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\StepupMiddleware\ManagementBundle\Validator;
20
21
use Assert\Assertion as Assert;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Surfnet\StepupMiddleware...Bundle\Validator\Assert.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert;
23
24
class ServiceProviderConfigurationValidator implements ConfigurationValidatorInterface
25
{
26
    public function validate(array $configuration, $propertyPath)
27
    {
28
        Assert::isArray($configuration, 'invalid configuration format, must be an object', $propertyPath);
29
30
        $acceptedProperties = [
31
            'entity_id',
32
            'public_key',
33
            'acs',
34
            'loa',
35
            'assertion_encryption_enabled',
36
            'second_factor_only',
37
            'second_factor_only_nameid_patterns',
38
            'blacklisted_encryption_algorithms',
39
        ];
40
        StepupAssert::keysMatch(
41
            $configuration,
42
            $acceptedProperties,
43
            sprintf(
44
                "The following properties must be present: '%s'; other properties are not supported",
45
                join("', '", $acceptedProperties)
46
            ),
47
            $propertyPath
48
        );
49
50
        $this->validateStringValue($configuration, 'entity_id', $propertyPath);
51
        $this->validateStringValue($configuration, 'public_key', $propertyPath);
52
        $this->validateAssertionConsumerUrls($configuration, $propertyPath);
53
        $this->validateLoaDefinition($configuration, $propertyPath);
54
        $this->validateBooleanValue(
55
            $configuration,
56
            'assertion_encryption_enabled',
57
            $propertyPath
58
        );
59
        $this->validateBooleanValue(
60
            $configuration,
61
            'second_factor_only',
62
            $propertyPath
63
        );
64
        $this->validateListOfNameIdPatterns(
65
            $configuration,
66
            'second_factor_only_nameid_patterns',
67
            $propertyPath
68
        );
69
        $this->validateStringValues(
70
            $configuration,
71
            'blacklisted_encryption_algorithms',
72
            $propertyPath
73
        );
74
    }
75
76
    /**
77
     * @param array  $configuration
78
     * @param string $name
79
     * @param string $propertyPath
80
     */
81
    private function validateStringValue($configuration, $name, $propertyPath)
82
    {
83
        Assert::string($configuration[$name], 'value must be a string', $propertyPath . '.' . $name);
84
    }
85
86
    /**
87
     * @param array  $configuration
88
     * @param string $name
89
     * @param string $propertyPath
90
     */
91
    private function validateStringValues($configuration, $name, $propertyPath)
92
    {
93
        Assert::isArray($configuration[$name], 'value must be an array', $propertyPath . '.' . $name);
94
        Assert::allString($configuration[$name], 'value must be an array of strings', $propertyPath . '.' . $name);
95
    }
96
97
    /**
98
     * @param array  $configuration
99
     * @param string $name
100
     * @param string $propertyPath
101
     */
102
    private function validateBooleanValue($configuration, $name, $propertyPath)
103
    {
104
        Assert::boolean($configuration[$name], 'value must be a boolean', $propertyPath . '.' . $name);
105
    }
106
107
    /**
108
     * @param array  $configuration
109
     * @param string $propertyPath
110
     */
111
    private function validateAssertionConsumerUrls($configuration, $propertyPath)
112
    {
113
        $value = $configuration['acs'];
114
        $propertyPath = $propertyPath . '.acs';
115
116
        Assert::isArray($value, 'must contain a non-empty array of strings', $propertyPath);
117
        Assert::true(count($value) >= 1, 'array must contain at least one value', $propertyPath);
118
        Assert::allString($value, 'must be an array of strings', $propertyPath);
119
    }
120
121
    /**
122
     * @param array  $configuration
123
     * @param string $propertyPath
124
     */
125 View Code Duplication
    private function validateLoaDefinition($configuration, $propertyPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $value = $configuration['loa'];
128
        $path  = $propertyPath . '.loa';
129
130
        Assert::isArray($value, 'must be an object', $path);
131
        Assert::keyExists($value, '__default__', "must have the default loa set on the '__default__' property", $path);
132
        Assert::allString($value, 'all properties must contain strings as values', $path);
133
    }
134
135
    /**
136
     * @param array $configuration
137
     * @param string $name
138
     * @param string $propertyPath
139
     */
140
    private function validateListOfNameIdPatterns($configuration, $name, $propertyPath)
141
    {
142
        $value = $configuration[$name];
143
        $propertyPath = $propertyPath . '.' . $name;
144
145
        Assert::isArray($value, 'must contain an array', $propertyPath);
146
        Assert::allString($value, 'must be an array of strings', $propertyPath);
147
    }
148
}
149