Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.12% |
25 / 32 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| InstanceCopier | |
78.12% |
25 / 32 |
|
62.50% |
5 / 8 |
16.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withInstanceContentCopier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withPermissionCopier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| withEventManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withPermissionCopiers | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| transfer | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| copy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doCopy | |
80.00% |
16 / 20 |
|
0.00% |
0 / 1 |
6.29 | |||
| 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) 2022-2023 (original work) Open Assessment Technologies SA. |
| 19 | * |
| 20 | * @author Andrei Shapiro <andrei.shapiro@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\resources\Service; |
| 26 | |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | use oat\oatbox\event\EventManager; |
| 29 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 30 | use oat\tao\model\resources\Contract\ResourceTransferInterface; |
| 31 | use oat\tao\model\resources\Event\InstanceCopiedEvent; |
| 32 | use oat\tao\model\resources\ResourceTransferResult; |
| 33 | use RuntimeException; |
| 34 | use core_kernel_classes_Class; |
| 35 | use core_kernel_classes_Resource; |
| 36 | use oat\tao\model\resources\Contract\InstanceCopierInterface; |
| 37 | use oat\tao\model\resources\Contract\PermissionCopierInterface; |
| 38 | use oat\tao\model\resources\Contract\InstanceContentCopierInterface; |
| 39 | use oat\tao\model\resources\Contract\InstanceMetadataCopierInterface; |
| 40 | |
| 41 | class InstanceCopier implements InstanceCopierInterface, ResourceTransferInterface |
| 42 | { |
| 43 | private InstanceMetadataCopierInterface $instanceMetadataCopier; |
| 44 | private InstanceContentCopierInterface $instanceContentCopier; |
| 45 | private PermissionCopierInterface $permissionCopier; |
| 46 | private Ontology $ontology; |
| 47 | private EventManager $eventManager; |
| 48 | |
| 49 | public function __construct(InstanceMetadataCopierInterface $instanceMetadataCopier, Ontology $ontology) |
| 50 | { |
| 51 | $this->instanceMetadataCopier = $instanceMetadataCopier; |
| 52 | $this->ontology = $ontology; |
| 53 | } |
| 54 | |
| 55 | public function withInstanceContentCopier(InstanceContentCopierInterface $instanceContentCopier): void |
| 56 | { |
| 57 | $this->instanceContentCopier = $instanceContentCopier; |
| 58 | } |
| 59 | |
| 60 | public function withPermissionCopier(PermissionCopierInterface $permissionCopier): void |
| 61 | { |
| 62 | $this->permissionCopier = $permissionCopier; |
| 63 | } |
| 64 | |
| 65 | public function withEventManager(EventManager $eventManager): void |
| 66 | { |
| 67 | $this->eventManager = $eventManager; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * This method is to be used with tagged_iterator() from service providers |
| 72 | * (but only the last copier from the iterable is effectively applied). |
| 73 | */ |
| 74 | public function withPermissionCopiers(iterable $copiers): void |
| 75 | { |
| 76 | foreach ($copiers as $copier) { |
| 77 | $this->withPermissionCopier($copier); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public function transfer(ResourceTransferCommand $command): ResourceTransferResult |
| 82 | { |
| 83 | $instance = $this->ontology->getResource($command->getFrom()); |
| 84 | $destinationClass = $this->ontology->getClass($command->getTo()); |
| 85 | $newInstance = $this->doCopy($instance, $destinationClass, $command->keepOriginalAcl()); |
| 86 | |
| 87 | return new ResourceTransferResult($newInstance->getUri()); |
| 88 | } |
| 89 | |
| 90 | public function copy( |
| 91 | core_kernel_classes_Resource $instance, |
| 92 | core_kernel_classes_Class $destinationClass |
| 93 | ): core_kernel_classes_Resource { |
| 94 | return $this->doCopy($instance, $destinationClass); |
| 95 | } |
| 96 | |
| 97 | private function doCopy( |
| 98 | core_kernel_classes_Resource $instance, |
| 99 | core_kernel_classes_Class $destinationClass, |
| 100 | bool $keepOriginalPermissions = true |
| 101 | ): core_kernel_classes_Resource { |
| 102 | $newInstance = $destinationClass->createInstance($instance->getLabel()); |
| 103 | |
| 104 | if ($newInstance === null) { |
| 105 | throw new RuntimeException( |
| 106 | sprintf( |
| 107 | 'New instance was not created. Original instance uri: %s, destination class uri: %s', |
| 108 | $instance->getUri(), |
| 109 | $destinationClass->getUri() |
| 110 | ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | $this->instanceMetadataCopier->copy($instance, $newInstance); |
| 115 | |
| 116 | if (isset($this->instanceContentCopier)) { |
| 117 | $this->instanceContentCopier->copy($instance, $newInstance); |
| 118 | } |
| 119 | |
| 120 | if (isset($this->permissionCopier)) { |
| 121 | $this->permissionCopier->copy( |
| 122 | $keepOriginalPermissions ? $instance : $destinationClass, |
| 123 | $newInstance |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | if (isset($this->eventManager)) { |
| 128 | $this->eventManager->trigger(new InstanceCopiedEvent($newInstance->getUri(), $instance->getUri())); |
| 129 | } |
| 130 | |
| 131 | return $newInstance; |
| 132 | } |
| 133 | } |