Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.91% |
17 / 23 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InstanceCopierProxy | |
73.91% |
17 / 23 |
|
80.00% |
4 / 5 |
10.44 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addInstanceCopier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| transfer | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getCopier | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getRootClassUri | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
8.74 | |||
| 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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\resources\Service; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use oat\generis\model\data\Ontology; |
| 27 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 28 | use oat\tao\model\resources\Contract\ResourceTransferInterface; |
| 29 | use oat\tao\model\resources\Contract\RootClassesListServiceInterface; |
| 30 | use oat\tao\model\resources\ResourceTransferResult; |
| 31 | use core_kernel_classes_Class; |
| 32 | |
| 33 | class InstanceCopierProxy implements ResourceTransferInterface |
| 34 | { |
| 35 | /** @var ResourceTransferInterface[] */ |
| 36 | private array $instanceCopiers; |
| 37 | private RootClassesListServiceInterface $rootClassesListService; |
| 38 | private Ontology $ontology; |
| 39 | |
| 40 | public function __construct(RootClassesListServiceInterface $rootClassesListService, Ontology $ontology) |
| 41 | { |
| 42 | $this->rootClassesListService = $rootClassesListService; |
| 43 | $this->ontology = $ontology; |
| 44 | } |
| 45 | |
| 46 | public function addInstanceCopier(string $rootClassUri, ResourceTransferInterface $copier): void |
| 47 | { |
| 48 | $this->instanceCopiers[$rootClassUri] = $copier; |
| 49 | } |
| 50 | |
| 51 | public function transfer(ResourceTransferCommand $command): ResourceTransferResult |
| 52 | { |
| 53 | $destinationClass = $this->ontology->getClass($command->getTo()); |
| 54 | |
| 55 | return $this->getCopier($destinationClass)->transfer($command); |
| 56 | } |
| 57 | |
| 58 | private function getCopier(core_kernel_classes_Class $class): ResourceTransferInterface |
| 59 | { |
| 60 | $rootClasUri = $this->getRootClassUri($class); |
| 61 | |
| 62 | if (isset($this->instanceCopiers[$rootClasUri])) { |
| 63 | return $this->instanceCopiers[$rootClasUri]; |
| 64 | } |
| 65 | |
| 66 | throw new InvalidArgumentException( |
| 67 | sprintf( |
| 68 | 'There is no instance copier mapper for root class %s', |
| 69 | $rootClasUri |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | private function getRootClassUri(core_kernel_classes_Class $class): string |
| 75 | { |
| 76 | foreach ($this->rootClassesListService->list() as $rootClass) { |
| 77 | if ($class->equals($rootClass) || $class->isSubClassOf($rootClass)) { |
| 78 | return $rootClass->getUri(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | throw new InvalidArgumentException( |
| 83 | sprintf( |
| 84 | 'Provided class %s does not belong to any root class', |
| 85 | $class->getUri() |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | } |