validateEmailTemplatesConfiguration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ManagementBundle\Validator;
20
21
use Assert\Assertion;
22
use Assert\AssertionFailedException;
23
use Assert\InvalidArgumentException as AssertionException;
24
use InvalidArgumentException as CoreInvalidArgumentException;
25
use Surfnet\Stepup\Helper\JsonHelper;
26
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert;
27
use Symfony\Component\Validator\Constraint;
28
use Symfony\Component\Validator\ConstraintValidator;
29
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
30
use TypeError;
31
32
/**
33
 * Once the Assert 2.0 library has been built this should be converted to the lazy assertions so we can report
34
 * all errors at once.
35
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
36
class ConfigurationStructureValidator extends ConstraintValidator
37
{
38
    public function __construct(
39
        private readonly GatewayConfigurationValidator $gatewayConfigurationValidator,
40
        private readonly EmailTemplatesConfigurationValidator $emailTemplatesConfigurationValidator,
41
    ) {
42
    }
43
44
    public function validate(mixed $value, Constraint $constraint): void
45
    {
46
        /** @var ConstraintViolationBuilder|false $violation */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
47
        $violation = false;
48
49
        try {
50
            $decoded = $this->decodeJson($value);
51
            $this->validateRoot($decoded);
52
        } catch (AssertionException $exception) {
53
            // method is not in the interface yet, but the old method is deprecated.
54
            $violation = $this->context->buildViolation($exception->getMessage());
55
            $violation->atPath($exception->getPropertyPath());
0 ignored issues
show
Bug introduced by
It seems like $exception->getPropertyPath() can also be of type null; however, parameter $path of Symfony\Component\Valida...lderInterface::atPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            $violation->atPath(/** @scrutinizer ignore-type */ $exception->getPropertyPath());
Loading history...
56
        } catch (CoreInvalidArgumentException|TypeError $exception) {
57
            $violation = $this->context->buildViolation($exception->getMessage());
58
        }
59
60
        if ($violation) {
61
            // ensure we have a sensible path.
62
            $violation->addViolation();
63
        }
64
    }
65
66
    private function decodeJson(string $rawValue): mixed
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationStructureValidator::decodeJson" must be prefixed with an underscore
Loading history...
67
    {
68
        return JsonHelper::decode($rawValue);
69
    }
70
71
    public function validateRoot(array $configuration): void
72
    {
73
        $acceptedProperties = ['gateway', 'sraa', 'email_templates'];
74
        StepupAssert::keysMatch(
75
            $configuration,
76
            $acceptedProperties,
77
            sprintf("Expected only properties '%s'", implode(',', $acceptedProperties)),
78
            '(root)',
79
        );
80
81
        $this->validateGatewayConfiguration($configuration, 'gateway');
82
        $this->validateSraaConfiguration($configuration, 'sraa');
83
        $this->validateEmailTemplatesConfiguration($configuration, 'email_templates');
84
    }
85
86
    private function validateGatewayConfiguration(array $configuration, string $propertyPath): void
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationStructureValidator::validateGatewayConfiguration" must be prefixed with an underscore
Loading history...
87
    {
88
        Assertion::isArray($configuration['gateway'], 'Property "gateway" must have an object as value', $propertyPath);
89
90
        $this->gatewayConfigurationValidator->validate($configuration['gateway'], $propertyPath);
91
    }
92
93
    private function validateSraaConfiguration(array $configuration, string $propertyPath): void
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationStructureValidator::validateSraaConfiguration" must be prefixed with an underscore
Loading history...
94
    {
95
        Assertion::isArray(
96
            $configuration['sraa'],
97
            'Property sraa must have an array of name_ids (string) as value',
98
            $propertyPath,
99
        );
100
101
        foreach ($configuration['sraa'] as $index => $value) {
102
            Assertion::string(
103
                $value,
104
                'value must be a string (the name_id of the SRAA)',
105
                $propertyPath . '[' . $index . ']',
106
            );
107
        }
108
    }
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $propertyPath should have a doc-comment as per coding-style.
Loading history...
111
     * @param array<string, mixed> $configuration
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
112
     * @throws AssertionFailedException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
113
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
114
    private function validateEmailTemplatesConfiguration(array $configuration, string $propertyPath): void
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationStructureValidator::validateEmailTemplatesConfiguration" must be prefixed with an underscore
Loading history...
115
    {
116
        Assertion::isArray(
117
            $configuration['email_templates'],
118
            'Property "email_templates" must have an object as value',
119
            $propertyPath,
120
        );
121
122
        $this->emailTemplatesConfigurationValidator->validate($configuration['email_templates'], $propertyPath);
123
    }
124
}
125