Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
24 / 27 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ClassPropertiesChangedListener | |
88.89% |
24 / 27 |
|
50.00% |
1 / 2 |
6.05 | |
0.00% |
0 / 1 |
| handleEvent | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
4 | |||
| handleUpdatedEvent | |
0.00% |
0 / 3 |
|
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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\listener; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use oat\generis\model\data\event\ResourceUpdated; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\tao\model\AdvancedSearch\AdvancedSearchChecker; |
| 29 | use oat\tao\model\dto\OldProperty; |
| 30 | use oat\tao\model\event\ClassPropertiesChangedEvent; |
| 31 | use oat\tao\model\search\tasks\RenameIndexProperties; |
| 32 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 33 | use RuntimeException; |
| 34 | |
| 35 | class ClassPropertiesChangedListener extends ConfigurableService |
| 36 | { |
| 37 | public const SERVICE_ID = 'tao/ClassPropertiesChangedListener'; |
| 38 | |
| 39 | public function handleEvent(ClassPropertiesChangedEvent $event): void |
| 40 | { |
| 41 | if (!$this->getServiceLocator()->get(AdvancedSearchChecker::class)->isEnabled()) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | $taskMessage = __('Updating search index'); |
| 46 | |
| 47 | $queueDispatcher = $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
| 48 | $queueDispatcher->createTask( |
| 49 | new RenameIndexProperties(), |
| 50 | array_map( |
| 51 | function (array $property) { |
| 52 | if (!isset($property['oldProperty'], $property['property'])) { |
| 53 | throw new RuntimeException('property data is empty'); |
| 54 | } |
| 55 | |
| 56 | /** @var core_kernel_classes_Property $propertyObject */ |
| 57 | $propertyObject = $property['property']; |
| 58 | |
| 59 | /** @var OldProperty $oldProperty */ |
| 60 | $oldProperty = $property['oldProperty']; |
| 61 | |
| 62 | $oldPropertyType = $oldProperty->getPropertyType(); |
| 63 | |
| 64 | return [ |
| 65 | 'uri' => $propertyObject->getUri(), |
| 66 | 'oldLabel' => $oldProperty->getLabel(), |
| 67 | 'oldAlias' => $oldProperty->getAlias(), |
| 68 | 'oldPropertyType' => $oldPropertyType === null ? null : $oldPropertyType->getUri(), |
| 69 | ]; |
| 70 | }, |
| 71 | $event->getProperties() |
| 72 | ), |
| 73 | $taskMessage |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | public function handleUpdatedEvent(ResourceUpdated $event): void |
| 78 | { |
| 79 | if ($event->getResource()->isProperty()) { |
| 80 | $classProperty = new \core_kernel_classes_Property($event->getResource()); |
| 81 | $classProperty->clearCachedValues(); |
| 82 | } |
| 83 | } |
| 84 | } |