Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.85% |
48 / 65 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| MetadataPropertiesValidator | |
73.85% |
48 / 65 |
|
83.33% |
5 / 6 |
23.80 | |
0.00% |
0 / 1 |
| validateMetadataExistence | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| validateMetadataUniqueness | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| validateMetadataTypes | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| validateMetadataIsStatistical | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| buildHeaderException | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| throwErrorOrAggregatedException | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| 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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\Import\Validator; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use oat\tao\model\StatisticalMetadata\Contract\Header; |
| 27 | use oat\tao\model\StatisticalMetadata\Import\Exception\HeaderValidationException; |
| 28 | use oat\tao\model\StatisticalMetadata\Import\Exception\AggregatedValidationException; |
| 29 | use tao_helpers_form_elements_Htmlarea; |
| 30 | use tao_helpers_form_elements_Textarea; |
| 31 | use tao_helpers_form_elements_Textbox; |
| 32 | |
| 33 | class MetadataPropertiesValidator |
| 34 | { |
| 35 | private const ALLOWED_WIDGETS = [ |
| 36 | tao_helpers_form_elements_Textbox::WIDGET_ID, |
| 37 | tao_helpers_form_elements_Textarea::WIDGET_ID, |
| 38 | tao_helpers_form_elements_Htmlarea::WIDGET_ID |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * @param core_kernel_classes_Property[] $metadataProperties |
| 43 | * |
| 44 | * @throws AggregatedValidationException |
| 45 | * @throws HeaderValidationException |
| 46 | */ |
| 47 | public function validateMetadataExistence(array $aliases, array $metadataProperties): void |
| 48 | { |
| 49 | $existingAliases = []; |
| 50 | |
| 51 | foreach ($metadataProperties as $metadataProperty) { |
| 52 | $existingAliases[] = $metadataProperty->getAlias(); |
| 53 | } |
| 54 | |
| 55 | $exceptions = []; |
| 56 | |
| 57 | foreach ($aliases as $alias) { |
| 58 | if (!in_array($alias, $existingAliases, true)) { |
| 59 | $exceptions[] = $this->buildHeaderException($alias, 'Property referenced by "%s" not found'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $this->throwErrorOrAggregatedException( |
| 64 | $exceptions, |
| 65 | $aliases, |
| 66 | 'None of properties referenced by "%s" columns were found' |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param core_kernel_classes_Property[] $metadataProperties |
| 72 | * |
| 73 | * @throws AggregatedValidationException |
| 74 | * @throws HeaderValidationException |
| 75 | */ |
| 76 | public function validateMetadataUniqueness(array $metadataProperties): void |
| 77 | { |
| 78 | $existingAliases = []; |
| 79 | $exceptions = []; |
| 80 | |
| 81 | foreach ($metadataProperties as $metadataProperty) { |
| 82 | $alias = $metadataProperty->getAlias(); |
| 83 | |
| 84 | if (!in_array($alias, $existingAliases, true)) { |
| 85 | $existingAliases[] = $alias; |
| 86 | |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if (!array_key_exists($alias, $exceptions)) { |
| 91 | $exceptions[$alias] = $this->buildHeaderException( |
| 92 | $alias ?? $metadataProperty->getLabel(), |
| 93 | 'Property referenced by "%s" is not unique' |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $this->throwErrorOrAggregatedException( |
| 99 | $exceptions, |
| 100 | $existingAliases, |
| 101 | 'None of properties referenced by "%s" columns are unique' |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param core_kernel_classes_Property[] $metadataProperties |
| 107 | * |
| 108 | * @throws AggregatedValidationException |
| 109 | * @throws HeaderValidationException |
| 110 | */ |
| 111 | public function validateMetadataTypes(array $metadataProperties): void |
| 112 | { |
| 113 | $exceptions = []; |
| 114 | |
| 115 | foreach ($metadataProperties as $metadataProperty) { |
| 116 | if (!in_array($metadataProperty->getWidget()->getUri(), self::ALLOWED_WIDGETS, true)) { |
| 117 | $exceptions[] = $this->buildHeaderException( |
| 118 | $metadataProperty->getAlias() ?? $metadataProperty->getLabel(), |
| 119 | 'Property referenced by "%s" has invalid input type - only TEXT is allowed' |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $this->throwErrorOrAggregatedException( |
| 125 | $exceptions, |
| 126 | $metadataProperties, |
| 127 | 'None of properties referenced by "%s" columns have a valid input type - only TEXT is allowed' |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param core_kernel_classes_Property[] $metadataProperties |
| 133 | * |
| 134 | * @throws AggregatedValidationException |
| 135 | * @throws HeaderValidationException |
| 136 | */ |
| 137 | public function validateMetadataIsStatistical(array $metadataProperties): void |
| 138 | { |
| 139 | $exceptions = []; |
| 140 | |
| 141 | foreach ($metadataProperties as $metadataProperty) { |
| 142 | if (!$metadataProperty->isStatistical()) { |
| 143 | $exceptions[] = $this->buildHeaderException( |
| 144 | $metadataProperty->getAlias() ?? $metadataProperty->getLabel(), |
| 145 | 'Property referenced by "%s" must be "statistical"' |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $this->throwErrorOrAggregatedException( |
| 151 | $exceptions, |
| 152 | $metadataProperties, |
| 153 | 'None of properties referenced by "%s" columns are statistical' |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | private function buildHeaderException(string $alias, string $message): HeaderValidationException |
| 158 | { |
| 159 | $column = Header::METADATA_PREFIX . $alias; |
| 160 | |
| 161 | $exception = new HeaderValidationException($message, [$column]); |
| 162 | $exception->setColumn($column); |
| 163 | |
| 164 | return $exception; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param HeaderValidationException[] $exceptions |
| 169 | */ |
| 170 | private function throwErrorOrAggregatedException(array $exceptions, array $valuesToCheck, string $message): void |
| 171 | { |
| 172 | if (empty($exceptions)) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (count($valuesToCheck) === count($exceptions)) { |
| 177 | throw new HeaderValidationException( |
| 178 | $message, |
| 179 | [Header::METADATA_PREFIX . '{property_alias}'] |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | throw new AggregatedValidationException($exceptions, []); |
| 184 | } |
| 185 | } |