Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
LtiProviderValidator | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
11.02 | |
0.00% |
0 / 1 |
validateArray | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
validateMappedField | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
getConfigurationMapper | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValidationFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValidationRegistry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoLti\models\classes\LtiProvider\Validation; |
24 | |
25 | use InvalidArgumentException; |
26 | use oat\oatbox\service\ConfigurableService; |
27 | use oat\taoLti\models\classes\LtiProvider\LtiProviderFieldsMapper; |
28 | |
29 | class LtiProviderValidator extends ConfigurableService |
30 | { |
31 | /** |
32 | * @var array |
33 | */ |
34 | private $errors = []; |
35 | |
36 | /** |
37 | * @throw InvalidArgumentException |
38 | */ |
39 | public function validateArray(string $schema, array $data): void |
40 | { |
41 | $this->errors = []; |
42 | |
43 | foreach (array_keys($this->getValidationRegistry()->getValidators($schema)) as $field) { |
44 | $mappedField = $this->getConfigurationMapper()->map($field); |
45 | |
46 | if (!$mappedField) { |
47 | continue; |
48 | } |
49 | |
50 | foreach ($this->getValidationFactory()->createFormValidators($schema, $field) as $validators) { |
51 | $this->validateMappedField($validators, $data, $mappedField); |
52 | } |
53 | } |
54 | |
55 | if (!empty($this->errors)) { |
56 | $errors = $this->errors; |
57 | $this->errors = []; |
58 | throw new InvalidArgumentException(implode(PHP_EOL, $errors)); |
59 | } |
60 | } |
61 | |
62 | private function validateMappedField(array $validators, array $data, string $mappedField): void |
63 | { |
64 | foreach ($validators as $validator) { |
65 | if ($validator->evaluate($data[$mappedField] ?? null)) { |
66 | continue; |
67 | } |
68 | $this->errors[] = sprintf('"%s": %s', $mappedField, $validator->getMessage()); |
69 | } |
70 | } |
71 | |
72 | private function getConfigurationMapper(): LtiProviderFieldsMapper |
73 | { |
74 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
75 | return $this->getServiceLocator()->get(LtiProviderFieldsMapper::class); |
76 | } |
77 | |
78 | private function getValidationFactory(): ValidatorsFactory |
79 | { |
80 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
81 | return $this->getServiceLocator()->get(ValidatorsFactory::class); |
82 | } |
83 | |
84 | private function getValidationRegistry(): ValidationRegistry |
85 | { |
86 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
87 | return $this->getServiceLocator()->get(ValidationRegistry::class); |
88 | } |
89 | } |