Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.50% |
33 / 40 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ClassCopierProxy | |
82.50% |
33 / 40 |
|
83.33% |
5 / 6 |
12.77 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addClassCopier | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| transfer | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| copy | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| getTransfer | |
30.00% |
3 / 10 |
|
0.00% |
0 / 1 |
3.37 | |||
| extractRootClass | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| 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 InvalidArgumentException; |
| 28 | use core_kernel_classes_Class; |
| 29 | use oat\generis\model\data\Ontology; |
| 30 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 31 | use oat\tao\model\resources\Contract\ClassCopierInterface; |
| 32 | use oat\tao\model\resources\Contract\ResourceTransferInterface; |
| 33 | use oat\tao\model\resources\Contract\RootClassesListServiceInterface; |
| 34 | use oat\tao\model\resources\ResourceTransferResult; |
| 35 | |
| 36 | class ClassCopierProxy implements ClassCopierInterface, ResourceTransferInterface |
| 37 | { |
| 38 | private RootClassesListServiceInterface $rootClassesListService; |
| 39 | |
| 40 | /** @var array<string, ClassCopierInterface|ResourceTransferInterface> */ |
| 41 | private array $classCopiers = []; |
| 42 | |
| 43 | private Ontology $ontology; |
| 44 | |
| 45 | public function __construct(RootClassesListServiceInterface $rootClassesListService, Ontology $ontology) |
| 46 | { |
| 47 | $this->rootClassesListService = $rootClassesListService; |
| 48 | $this->ontology = $ontology; |
| 49 | } |
| 50 | |
| 51 | public function addClassCopier(string $rootClassUri, ResourceTransferInterface $classCopier): void |
| 52 | { |
| 53 | if (!in_array($rootClassUri, $this->rootClassesListService->listUris(), true)) { |
| 54 | throw new InvalidArgumentException('Provided root class URI was not found in root classes list.'); |
| 55 | } |
| 56 | |
| 57 | if (isset($this->classCopiers[$rootClassUri])) { |
| 58 | throw new InvalidArgumentException( |
| 59 | sprintf( |
| 60 | 'Root class (%s) already configured to use copier service (%s)', |
| 61 | $rootClassUri, |
| 62 | get_class($this->classCopiers[$rootClassUri]) |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | $this->classCopiers[$rootClassUri] = $classCopier; |
| 68 | } |
| 69 | |
| 70 | public function transfer(ResourceTransferCommand $command): ResourceTransferResult |
| 71 | { |
| 72 | return $this->getTransfer( |
| 73 | $this->ontology->getClass($command->getFrom()), |
| 74 | $command->getTo() |
| 75 | )->transfer($command); |
| 76 | } |
| 77 | |
| 78 | public function copy( |
| 79 | core_kernel_classes_Class $class, |
| 80 | core_kernel_classes_Class $destinationClass |
| 81 | ): core_kernel_classes_Class { |
| 82 | $result = $this->transfer( |
| 83 | new ResourceTransferCommand( |
| 84 | $class->getUri(), |
| 85 | $destinationClass->getUri(), |
| 86 | ResourceTransferCommand::ACL_KEEP_ORIGINAL, |
| 87 | ResourceTransferCommand::TRANSFER_MODE_COPY |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | return $this->ontology->getClass($result->getDestination()); |
| 92 | } |
| 93 | |
| 94 | private function getTransfer(core_kernel_classes_Class $from, string $toUri): ResourceTransferInterface |
| 95 | { |
| 96 | $rootClassUri = $this->extractRootClass($from)->getUri(); |
| 97 | |
| 98 | if (isset($this->classCopiers[$rootClassUri])) { |
| 99 | return $this->classCopiers[$rootClassUri]; |
| 100 | } |
| 101 | |
| 102 | throw new InvalidArgumentException( |
| 103 | sprintf( |
| 104 | 'Class (%s) cannot be copied to the class (%s) - not supported by any class copier.', |
| 105 | $from->getUri(), |
| 106 | $toUri |
| 107 | ) |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | private function extractRootClass(core_kernel_classes_Class $class): core_kernel_classes_Class |
| 112 | { |
| 113 | foreach ($this->rootClassesListService->list() as $rootClass) { |
| 114 | if ($class->equals($rootClass) || $class->isSubClassOf($rootClass)) { |
| 115 | return $rootClass; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | throw new InvalidArgumentException('Provided class does not belong to any root class'); |
| 120 | } |
| 121 | } |