Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
59.09% |
13 / 22 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AssetClassCopier | |
59.09% |
13 / 22 |
|
75.00% |
3 / 4 |
6.71 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| transfer | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| copy | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| assertInAssetsRootClass | |
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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\classes\Copier; |
| 24 | |
| 25 | use oat\generis\model\data\Ontology; |
| 26 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 27 | use oat\tao\model\resources\Contract\ClassCopierInterface; |
| 28 | use oat\tao\model\resources\Contract\ResourceTransferInterface; |
| 29 | use oat\tao\model\resources\ResourceTransferResult; |
| 30 | use oat\tao\model\Specification\ClassSpecificationInterface; |
| 31 | use oat\taoMediaManager\model\TaoMediaOntology; |
| 32 | use core_kernel_classes_Class; |
| 33 | use InvalidArgumentException; |
| 34 | |
| 35 | class AssetClassCopier implements ClassCopierInterface, ResourceTransferInterface |
| 36 | { |
| 37 | private ClassSpecificationInterface $mediaClassSpecification; |
| 38 | private ResourceTransferInterface $taoClassCopier; |
| 39 | private Ontology $ontology; |
| 40 | |
| 41 | public function __construct( |
| 42 | ClassSpecificationInterface $mediaClassSpecification, |
| 43 | ResourceTransferInterface $taoClassCopier, |
| 44 | Ontology $ontology |
| 45 | ) { |
| 46 | $this->mediaClassSpecification = $mediaClassSpecification; |
| 47 | $this->taoClassCopier = $taoClassCopier; |
| 48 | $this->ontology = $ontology; |
| 49 | } |
| 50 | |
| 51 | public function transfer(ResourceTransferCommand $command): ResourceTransferResult |
| 52 | { |
| 53 | $this->assertInAssetsRootClass($this->ontology->getClass($command->getFrom())); |
| 54 | |
| 55 | return $this->taoClassCopier->transfer($command); |
| 56 | } |
| 57 | |
| 58 | public function copy( |
| 59 | core_kernel_classes_Class $class, |
| 60 | core_kernel_classes_Class $destinationClass |
| 61 | ): core_kernel_classes_Class { |
| 62 | $result = $this->transfer( |
| 63 | new ResourceTransferCommand( |
| 64 | $class->getUri(), |
| 65 | $destinationClass->getUri(), |
| 66 | ResourceTransferCommand::ACL_KEEP_ORIGINAL, |
| 67 | ResourceTransferCommand::TRANSFER_MODE_COPY |
| 68 | ) |
| 69 | ); |
| 70 | |
| 71 | return $this->ontology->getClass($result->getDestination()); |
| 72 | } |
| 73 | |
| 74 | private function assertInAssetsRootClass(core_kernel_classes_Class $class): void |
| 75 | { |
| 76 | if (!$this->mediaClassSpecification->isSatisfiedBy($class)) { |
| 77 | throw new InvalidArgumentException( |
| 78 | sprintf( |
| 79 | 'Class (%s) is not supported. Only classes from (%s) are supported', |
| 80 | $class->getUri(), |
| 81 | TaoMediaOntology::CLASS_URI_MEDIA_ROOT |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 | } |