Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| RenameIndexProperties | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
56 | |||
| 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-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\search\tasks; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use common_report_Report; |
| 27 | use core_kernel_classes_Property; |
| 28 | use core_kernel_persistence_Exception; |
| 29 | use oat\oatbox\action\Action; |
| 30 | use oat\oatbox\log\LoggerAwareTrait; |
| 31 | use oat\tao\model\search\index\IndexUpdaterInterface; |
| 32 | use oat\tao\model\taskQueue\Task\TaskAwareInterface; |
| 33 | use oat\tao\model\taskQueue\Task\TaskAwareTrait; |
| 34 | use Throwable; |
| 35 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 36 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 37 | |
| 38 | class RenameIndexProperties implements Action, ServiceLocatorAwareInterface, TaskAwareInterface |
| 39 | { |
| 40 | use ServiceLocatorAwareTrait; |
| 41 | use TaskAwareTrait; |
| 42 | use LoggerAwareTrait; |
| 43 | use IndexTrait; |
| 44 | |
| 45 | /** |
| 46 | * @param $properties |
| 47 | * |
| 48 | * @return common_report_Report |
| 49 | * @throws common_Exception |
| 50 | * @throws core_kernel_persistence_Exception |
| 51 | */ |
| 52 | public function __invoke($properties): common_report_Report |
| 53 | { |
| 54 | $indexProperties = []; |
| 55 | foreach ($properties as $propertyData) { |
| 56 | if ( |
| 57 | !isset( |
| 58 | $propertyData['oldLabel'], |
| 59 | $propertyData['oldAlias'], |
| 60 | $propertyData['oldPropertyType'], |
| 61 | $propertyData['uri'] |
| 62 | ) |
| 63 | ) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | $property = new core_kernel_classes_Property($propertyData['uri']); |
| 68 | $domain = $property->getDomain(); |
| 69 | if (null === $domain) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | $firstDomain = $domain->get(0); |
| 74 | if (null === $firstDomain) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | $type = $firstDomain->getUri(); |
| 79 | |
| 80 | /** @var string[] $parentClasses */ |
| 81 | /** @noinspection PhpParamsInspection */ |
| 82 | $parentClasses = $this->getParentClasses($firstDomain); |
| 83 | |
| 84 | /** @var core_kernel_classes_Property $propertyType */ |
| 85 | $propertyType = $this->getPropertyType($property); |
| 86 | if (null === $propertyType) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | $indexProperties[] = [ |
| 91 | 'type' => $type, |
| 92 | 'parentClasses' => $parentClasses, |
| 93 | 'oldName' => $this->getPropertyRealName($propertyData['oldLabel'], $propertyData['oldPropertyType']), |
| 94 | 'newName' => $this->getPropertyRealName($property->getLabel(), $propertyType->getUri()) |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | $this->logDebug( |
| 99 | sprintf( |
| 100 | 'Indexing %d properties: %s', |
| 101 | count($indexProperties), |
| 102 | var_export($indexProperties, true) |
| 103 | ) |
| 104 | ); |
| 105 | |
| 106 | try { |
| 107 | /** @var IndexUpdaterInterface $indexUpdater */ |
| 108 | $indexUpdater = $this->getServiceLocator()->get(IndexUpdaterInterface::SERVICE_ID); |
| 109 | $indexUpdater->updatePropertiesName($indexProperties); |
| 110 | } catch (Throwable $exception) { |
| 111 | $message = 'Failed during update search index'; |
| 112 | $this->logError($message); |
| 113 | |
| 114 | return common_report_Report::createFailure(__($message)); |
| 115 | } |
| 116 | |
| 117 | $message = 'Search index was successfully updated.'; |
| 118 | $this->logInfo($message); |
| 119 | |
| 120 | return common_report_Report::createSuccess(__($message)); |
| 121 | } |
| 122 | } |