Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
10.34% |
6 / 58 |
|
12.50% |
1 / 8 |
CRAP | |
0.00% |
0 / 1 |
| DependsOnPropertySynchronizer | |
10.34% |
6 / 58 |
|
12.50% |
1 / 8 |
251.49 | |
0.00% |
0 / 1 |
| sync | |
26.32% |
5 / 19 |
|
0.00% |
0 / 1 |
10.40 | |||
| incrementWithParents | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| getDependentProperties | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getNewValidationRules | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getRulesToRemove | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| bindProperties | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getFeatureFlagChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDependentPropertiesRepository | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Business\Service; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\generis\model\OntologyAwareTrait; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\tao\model\Context\ContextInterface; |
| 30 | use oat\tao\helpers\form\ValidationRuleRegistry; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 32 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 33 | use tao_models_classes_dataBinding_GenerisInstanceDataBinder; |
| 34 | use oat\tao\model\Lists\DataAccess\Repository\DependentPropertiesRepository; |
| 35 | use oat\tao\model\Lists\Business\Domain\DependentPropertiesRepositoryContext; |
| 36 | use oat\tao\model\Lists\Business\Domain\DependsOnPropertySynchronizerContext; |
| 37 | use oat\tao\model\Lists\Business\Contract\DependsOnPropertySynchronizerInterface; |
| 38 | use oat\tao\model\Lists\Business\Contract\DependentPropertiesRepositoryInterface; |
| 39 | |
| 40 | class DependsOnPropertySynchronizer extends ConfigurableService implements DependsOnPropertySynchronizerInterface |
| 41 | { |
| 42 | use OntologyAwareTrait; |
| 43 | |
| 44 | private const REQUIRED_VALIDATION_RULES = ['notEmpty']; |
| 45 | |
| 46 | /** @var DependentPropertiesRepositoryInterface */ |
| 47 | private $dependentPropertiesRepository; |
| 48 | |
| 49 | /** @var array[] */ |
| 50 | private $dependentProperties = []; |
| 51 | |
| 52 | /** @var array[] */ |
| 53 | private $rulesToRemove = []; |
| 54 | |
| 55 | public function sync(ContextInterface $context): void |
| 56 | { |
| 57 | $isListsDependencyEnabled = $this->getFeatureFlagChecker()->isEnabled( |
| 58 | FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED |
| 59 | ); |
| 60 | |
| 61 | if (!$isListsDependencyEnabled) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | /** @var core_kernel_classes_Property[] $properties */ |
| 66 | $properties = $context->getParameter(DependsOnPropertySynchronizerContext::PARAM_PROPERTIES, []); |
| 67 | $validationRuleProperty = $this->getProperty(ValidationRuleRegistry::PROPERTY_VALIDATION_RULE); |
| 68 | |
| 69 | foreach ($this->incrementWithParents($properties) as $property) { |
| 70 | foreach ($this->getDependentProperties($property) as $dependentProperty) { |
| 71 | $this->bindProperties( |
| 72 | $dependentProperty, |
| 73 | [ |
| 74 | ValidationRuleRegistry::PROPERTY_VALIDATION_RULE => $this->getNewValidationRules( |
| 75 | $property, |
| 76 | $dependentProperty, |
| 77 | $validationRuleProperty |
| 78 | ), |
| 79 | ] |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param core_kernel_classes_Property[] $properties |
| 87 | */ |
| 88 | private function incrementWithParents(array $properties): array |
| 89 | { |
| 90 | $parentProperties = []; |
| 91 | $initialProperties = []; |
| 92 | |
| 93 | foreach ($properties as $property) { |
| 94 | $initialProperties[] = $property->getUri(); |
| 95 | $parentProperty = $property->getDependsOnPropertyCollection()->current(); |
| 96 | |
| 97 | if ($parentProperty) { |
| 98 | $parentProperties[$parentProperty->getUri()] = $parentProperty; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | foreach ($parentProperties as $parentPropertyUri => $parentProperty) { |
| 103 | if (in_array($parentPropertyUri, $initialProperties, true)) { |
| 104 | unset($parentProperties[$parentPropertyUri]); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return array_merge($properties, $parentProperties); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return core_kernel_classes_Resource[] |
| 113 | */ |
| 114 | private function getDependentProperties(core_kernel_classes_Property $property): array |
| 115 | { |
| 116 | $propertyUri = $property->getUri(); |
| 117 | |
| 118 | if (!isset($this->dependentProperties[$propertyUri])) { |
| 119 | $this->dependentProperties[$propertyUri] = $this->getDependentPropertiesRepository()->findAll( |
| 120 | new DependentPropertiesRepositoryContext([ |
| 121 | DependentPropertiesRepositoryContext::PARAM_PROPERTY => $property, |
| 122 | ]) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return $this->dependentProperties[$propertyUri]; |
| 127 | } |
| 128 | |
| 129 | private function getNewValidationRules( |
| 130 | core_kernel_classes_Property $property, |
| 131 | core_kernel_classes_Resource $dependentProperty, |
| 132 | core_kernel_classes_Property $validationRuleProperty |
| 133 | ): array { |
| 134 | return array_diff( |
| 135 | $dependentProperty->getPropertyValues($validationRuleProperty), |
| 136 | $this->getRulesToRemove($property, $validationRuleProperty) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | private function getRulesToRemove( |
| 141 | core_kernel_classes_Property $property, |
| 142 | core_kernel_classes_Property $validationRuleProperty |
| 143 | ): array { |
| 144 | $propertyUri = $property->getUri(); |
| 145 | |
| 146 | if (!isset($this->rulesToRemove[$propertyUri])) { |
| 147 | $propertyValidationRules = $property->getPropertyValues($validationRuleProperty); |
| 148 | $this->rulesToRemove[$propertyUri] = array_diff( |
| 149 | self::REQUIRED_VALIDATION_RULES, |
| 150 | $propertyValidationRules |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | return $this->rulesToRemove[$propertyUri]; |
| 155 | } |
| 156 | |
| 157 | private function bindProperties(core_kernel_classes_Resource $property, array $values): void |
| 158 | { |
| 159 | $binder = new tao_models_classes_dataBinding_GenerisInstanceDataBinder($property); |
| 160 | $binder->bind($values); |
| 161 | } |
| 162 | |
| 163 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 164 | { |
| 165 | return $this->getServiceLocator()->get(FeatureFlagChecker::class); |
| 166 | } |
| 167 | |
| 168 | private function getDependentPropertiesRepository(): DependentPropertiesRepositoryInterface |
| 169 | { |
| 170 | if (!isset($this->dependentPropertiesRepository)) { |
| 171 | $this->dependentPropertiesRepository = $this->getServiceLocator()->get( |
| 172 | DependentPropertiesRepository::class |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | return $this->dependentPropertiesRepository; |
| 177 | } |
| 178 | } |