Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RemoveClassPropertyService | |
0.00% |
0 / 33 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| remove | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 | |||
| invalidatePropertyCache | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParentPropertyListCachedRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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\ClassProperty; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use oat\oatbox\event\EventManager; |
| 27 | use oat\generis\model\OntologyAwareTrait; |
| 28 | use oat\tao\model\Lists\DataAccess\Repository\ParentPropertyListCachedRepository; |
| 29 | use oat\tao\model\search\tasks\IndexTrait; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | use Psr\Http\Message\ServerRequestInterface; |
| 32 | use oat\tao\model\search\index\OntologyIndex; |
| 33 | use oat\tao\model\event\ClassPropertyRemovedEvent; |
| 34 | use oat\generis\model\data\event\ClassPropertyDeletedEvent; |
| 35 | |
| 36 | class RemoveClassPropertyService extends ConfigurableService |
| 37 | { |
| 38 | use OntologyAwareTrait; |
| 39 | use IndexTrait; |
| 40 | |
| 41 | public function remove(ServerRequestInterface $request): bool |
| 42 | { |
| 43 | $parsedBody = $request->getParsedBody(); |
| 44 | |
| 45 | $class = $this->getClass($parsedBody['classUri']); |
| 46 | $property = $this->getProperty($parsedBody['uri']); |
| 47 | $propertyType = $this->getPropertyType($property); |
| 48 | |
| 49 | if ($propertyType !== null) { |
| 50 | $propertyName = $this->getPropertyRealName($property->getLabel(), $propertyType->getUri()); |
| 51 | $this->getEventManager()->trigger(new ClassPropertyRemovedEvent($class, $propertyName)); |
| 52 | } |
| 53 | |
| 54 | $this->invalidatePropertyCache($property); |
| 55 | |
| 56 | // Delete property mode |
| 57 | foreach ($class->getProperties() as $classProperty) { |
| 58 | if ($classProperty->equals($property)) { |
| 59 | $indexes = $property->getPropertyValues($this->getProperty(OntologyIndex::PROPERTY_INDEX)); |
| 60 | |
| 61 | // Delete property and the existing values of this property |
| 62 | if ($property->delete(true)) { |
| 63 | $this->getEventManager()->trigger( |
| 64 | new ClassPropertyDeletedEvent( |
| 65 | $class, |
| 66 | [ |
| 67 | 'propertyUri' => $property->getUri() |
| 68 | ] |
| 69 | ) |
| 70 | ); |
| 71 | |
| 72 | // Delete index linked to the property |
| 73 | foreach ($indexes as $indexUri) { |
| 74 | $index = $this->getResource($indexUri); |
| 75 | $index->delete(true); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | private function invalidatePropertyCache(core_kernel_classes_Property $property): void |
| 88 | { |
| 89 | if ($property->getRange()) { |
| 90 | $this->getParentPropertyListCachedRepository()->deleteCache( |
| 91 | [ |
| 92 | 'listUri' => $property->getRange()->getUri() |
| 93 | ] |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private function getEventManager(): EventManager |
| 99 | { |
| 100 | return $this->getServiceManager()->get(EventManager::SERVICE_ID); |
| 101 | } |
| 102 | |
| 103 | private function getParentPropertyListCachedRepository(): ParentPropertyListCachedRepository |
| 104 | { |
| 105 | return $this->getServiceLocator()->get(ParentPropertyListCachedRepository::class); |
| 106 | } |
| 107 | } |