Completed
Push — develop ( b0b139...6288f9 )
by A.
9s
created

EmailTemplatesConfigurationValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
rs 9.0303
cc 3
eloc 32
nc 3
nop 2
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\Exception\InvalidArgumentException;
23
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert;
24
25
final class EmailTemplatesConfigurationValidator implements ConfigurationValidatorInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $requiredLocale;
31
32
    /**
33
     * @param string $requiredLocale
34
     */
35
    public function __construct($requiredLocale)
36
    {
37
        if (!is_string($requiredLocale)) {
38
            throw InvalidArgumentException::invalidType('string', 'defaultLocale', $requiredLocale);
39
        }
40
41
        $this->requiredLocale = $requiredLocale;
42
    }
43
44
    public function validate(array $configuration, $propertyPath)
45
    {
46
        $templateNames = [
47
            'confirm_email',
48
            'registration_code_with_ras',
49
            'registration_code_with_ra_locations',
50
            'vetted',
51
        ];
52
53
        StepupAssert::keysMatch(
54
            $configuration,
55
            $templateNames,
56
            sprintf("Expected only templates '%s'", join(',', $templateNames)),
57
            $propertyPath
58
        );
59
60
        foreach ($templateNames as $templateName) {
61
            Assert::isArray(
62
                $configuration[$templateName],
63
                'Property "' . $templateName . '" must have an object as value',
64
                $propertyPath
65
            );
66
67
            $templatePropertyPath = $propertyPath . '.' . $templateName;
68
69
            Assert::keyExists(
70
                $configuration[$templateName],
71
                $this->requiredLocale,
72
                "Required property '" . $this->requiredLocale . "' is missing",
73
                $templatePropertyPath
74
            );
75
76
            foreach ($configuration[$templateName] as $locale => $template) {
77
                $localePropertyPath = $templatePropertyPath . '[' . $locale . ']';
78
                Assert::string(
79
                    $locale,
80
                    'Locale must be string',
81
                    $localePropertyPath
82
                );
83
                Assert::string(
84
                    $template,
85
                    "Property '" . $this->requiredLocale . "' must have a string as value",
86
                    $localePropertyPath
87
                );
88
            }
89
        }
90
    }
91
}
92