IdentityProviderConfigurationValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateStringValue() 0 3 1
A validate() 0 25 2
A validateBooleanValue() 0 3 1
A validateLoaDefinition() 0 13 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\StepupMiddleware\ManagementBundle\Validator;
20
21
use Assert\Assertion;
22
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert;
23
24
class IdentityProviderConfigurationValidator implements ConfigurationValidatorInterface
25
{
26
    /**
27
     * @param array<string, mixed> $configuration
28
     */
29
    public function validate(array $configuration, string $propertyPath): void
30
    {
31
        $requiredProperties = [
32
            'entity_id',
33
            'loa',
34
            'use_pdp',
35
        ];
36
37
        if (empty($configuration['use_pdp'])) {
38
            $configuration['use_pdp'] = false;
39
        }
40
41
        StepupAssert::keysMatch(
42
            $configuration,
43
            $requiredProperties,
44
            sprintf(
45
                "The following properties must be present: '%s'; other properties are not supported",
46
                implode("', '", $requiredProperties),
47
            ),
48
            $propertyPath,
49
        );
50
51
        $this->validateStringValue($configuration, 'entity_id', $propertyPath);
52
        $this->validateLoaDefinition($configuration, $propertyPath);
53
        $this->validateBooleanValue($configuration, 'use_pdp', $propertyPath);
54
    }
55
56
    private function validateStringValue(array $configuration, string $name, string $propertyPath): void
57
    {
58
        Assertion::string($configuration[$name], 'value must be a string', $propertyPath . '.' . $name);
59
    }
60
61
    private function validateBooleanValue(array $configuration, string $name, string $propertyPath): void
62
    {
63
        Assertion::boolean($configuration[$name], 'value must be a boolean', $propertyPath . '.' . $name);
64
    }
65
66
    private function validateLoaDefinition(array $configuration, string $propertyPath): void
67
    {
68
        $value = $configuration['loa'];
69
        $path = $propertyPath . '.loa';
70
71
        Assertion::isArray($value, 'must be an object', $path);
72
        Assertion::keyExists(
73
            $value,
74
            '__default__',
75
            "must have the default loa set on the '__default__' property",
76
            $path,
77
        );
78
        Assertion::allString($value, 'all properties must contain strings as values', $path);
79
    }
80
}
81