Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.30% |
52 / 54 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ClassMover | |
96.30% |
52 / 54 |
|
88.89% |
8 / 9 |
23 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| withPermissionCopier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withPermissionCopiers | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| transfer | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| changePermissions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| assertIsNotRootClass | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| assertInSameRootClass | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
| assertIsNotSameClass | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| assertIsNotSubclass | |
100.00% |
8 / 8 |
|
100.00% |
1 / 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) 2023 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\resources\Service; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use InvalidArgumentException; |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | use oat\generis\model\OntologyRdfs; |
| 29 | use oat\oatbox\event\EventManager; |
| 30 | use oat\tao\model\event\ClassMovedEvent; |
| 31 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 32 | use oat\tao\model\resources\Contract\PermissionCopierInterface; |
| 33 | use oat\tao\model\resources\Contract\ResourceTransferInterface; |
| 34 | use oat\tao\model\resources\Contract\RootClassesListServiceInterface; |
| 35 | use oat\tao\model\resources\ResourceTransferResult; |
| 36 | use oat\tao\model\Specification\ClassSpecificationInterface; |
| 37 | |
| 38 | class ClassMover implements ResourceTransferInterface |
| 39 | { |
| 40 | private Ontology $ontology; |
| 41 | private ClassSpecificationInterface $rootClassSpecification; |
| 42 | private RootClassesListServiceInterface $rootClassesListService; |
| 43 | private EventManager $eventManager; |
| 44 | private PermissionCopierInterface $permissionCopier; |
| 45 | |
| 46 | public function __construct( |
| 47 | Ontology $ontology, |
| 48 | ClassSpecificationInterface $rootClassSpecification, |
| 49 | RootClassesListServiceInterface $rootClassesListService, |
| 50 | EventManager $eventManager |
| 51 | ) { |
| 52 | $this->ontology = $ontology; |
| 53 | $this->rootClassSpecification = $rootClassSpecification; |
| 54 | $this->rootClassesListService = $rootClassesListService; |
| 55 | $this->eventManager = $eventManager; |
| 56 | } |
| 57 | |
| 58 | public function withPermissionCopier(PermissionCopierInterface $permissionCopier): void |
| 59 | { |
| 60 | $this->permissionCopier = $permissionCopier; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * This method is to be used with tagged_iterator() from service providers |
| 65 | * (but only the last copier from the iterable is effectively applied). |
| 66 | */ |
| 67 | public function withPermissionCopiers(iterable $permissionCopiers): void |
| 68 | { |
| 69 | foreach ($permissionCopiers as $copier) { |
| 70 | $this->withPermissionCopier($copier); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function transfer(ResourceTransferCommand $command): ResourceTransferResult |
| 75 | { |
| 76 | $from = $this->ontology->getClass($command->getFrom()); |
| 77 | $to = $this->ontology->getClass($command->getTo()); |
| 78 | |
| 79 | $this->assertIsNotRootClass($from); |
| 80 | $this->assertInSameRootClass($from, $to); |
| 81 | $this->assertIsNotSameClass($from, $to); |
| 82 | $this->assertIsNotSubclass($from, $to); |
| 83 | |
| 84 | $status = $from->editPropertyValues($this->ontology->getProperty(OntologyRdfs::RDFS_SUBCLASSOF), $to); |
| 85 | |
| 86 | if ($status) { |
| 87 | $this->eventManager->trigger(new ClassMovedEvent($from)); |
| 88 | |
| 89 | if (isset($this->permissionCopier) && $command->useDestinationAcl()) { |
| 90 | $this->changePermissions($to, $from); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return new ResourceTransferResult($from->getUri()); |
| 95 | } |
| 96 | |
| 97 | private function changePermissions( |
| 98 | core_kernel_classes_Class $source, |
| 99 | core_kernel_classes_Class $destination |
| 100 | ): void { |
| 101 | $this->permissionCopier->copy($source, $destination); |
| 102 | |
| 103 | foreach ($destination->getInstances() as $instance) { |
| 104 | $this->permissionCopier->copy($source, $instance); |
| 105 | } |
| 106 | |
| 107 | foreach ($destination->getSubClasses() as $subClass) { |
| 108 | $this->changePermissions($destination, $subClass); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private function assertIsNotRootClass(core_kernel_classes_Class $class): void |
| 113 | { |
| 114 | if ($this->rootClassSpecification->isSatisfiedBy($class)) { |
| 115 | throw new InvalidArgumentException(sprintf('Root class "%s" cannot be moved', $class->getUri())); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private function assertInSameRootClass( |
| 120 | core_kernel_classes_Class $source, |
| 121 | core_kernel_classes_Class $destionation |
| 122 | ): void { |
| 123 | foreach ($this->rootClassesListService->list() as $rootClass) { |
| 124 | if ( |
| 125 | ($source->equals($rootClass) || $source->isSubClassOf($rootClass)) |
| 126 | && !$destionation->equals($rootClass) |
| 127 | && !$destionation->isSubClassOf($rootClass) |
| 128 | ) { |
| 129 | throw new InvalidArgumentException( |
| 130 | sprintf( |
| 131 | 'Selected class (%s) and destination class (%s) must be in the same root class (%s).', |
| 132 | $source->getUri(), |
| 133 | $destionation->getUri(), |
| 134 | $rootClass->getUri() |
| 135 | ) |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private function assertIsNotSameClass( |
| 142 | core_kernel_classes_Class $source, |
| 143 | core_kernel_classes_Class $destination |
| 144 | ): void { |
| 145 | if ($source->equals($destination)) { |
| 146 | throw new InvalidArgumentException( |
| 147 | sprintf( |
| 148 | 'Selected class (%s) and destination class (%s) cannot be the same class.', |
| 149 | $source->getUri(), |
| 150 | $destination->getUri() |
| 151 | ) |
| 152 | ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private function assertIsNotSubclass( |
| 157 | core_kernel_classes_Class $source, |
| 158 | core_kernel_classes_Class $destination |
| 159 | ): void { |
| 160 | if ($destination->isSubClassOf($source)) { |
| 161 | throw new InvalidArgumentException( |
| 162 | sprintf( |
| 163 | 'The destination class (%s) cannot be a subclass of the selected class (%s).', |
| 164 | $destination->getUri(), |
| 165 | $source->getUri() |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 | } |