Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemClassCopier
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 transfer
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 copy
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 assertInItemsRootClass
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
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
23declare(strict_types=1);
24
25namespace oat\taoItems\model\Copier;
26
27use InvalidArgumentException;
28use core_kernel_classes_Class;
29use oat\generis\model\data\Ontology;
30use oat\tao\model\resources\Command\ResourceTransferCommand;
31use oat\tao\model\resources\Contract\ResourceTransferInterface;
32use oat\tao\model\resources\ResourceTransferResult;
33use oat\tao\model\TaoOntology;
34use oat\tao\model\resources\Contract\ClassCopierInterface;
35
36class ItemClassCopier implements ClassCopierInterface, ResourceTransferInterface
37{
38    /** @var ClassCopierInterface|ResourceTransferInterface */
39    private $taoClassCopier;
40    private Ontology $ontology;
41
42    public function __construct(ResourceTransferInterface $taoClassCopier, Ontology $ontology)
43    {
44        $this->taoClassCopier = $taoClassCopier;
45        $this->ontology = $ontology;
46    }
47
48    public function transfer(ResourceTransferCommand $command): ResourceTransferResult
49    {
50        $this->assertInItemsRootClass($this->ontology->getClass($command->getFrom()));
51
52        return $this->taoClassCopier->transfer($command);
53    }
54
55    public function copy(
56        core_kernel_classes_Class $class,
57        core_kernel_classes_Class $destinationClass
58    ): core_kernel_classes_Class {
59        $result = $this->transfer(
60            new ResourceTransferCommand(
61                $class->getUri(),
62                $destinationClass->getUri(),
63                ResourceTransferCommand::ACL_KEEP_ORIGINAL,
64                ResourceTransferCommand::TRANSFER_MODE_COPY
65            )
66        );
67
68        return $this->ontology->getClass($result->getDestination());
69    }
70
71    private function assertInItemsRootClass(core_kernel_classes_Class $class): void
72    {
73        $rootClass = $class->getClass(TaoOntology::CLASS_URI_ITEM);
74
75        if (!$class->equals($rootClass) && !$class->isSubClassOf($rootClass)) {
76            throw new InvalidArgumentException(
77                sprintf(
78                    'Selected class (%s) is not supported because it is not part of the items root class (%s).',
79                    $class->getUri(),
80                    TaoOntology::CLASS_URI_ITEM
81                )
82            );
83        }
84    }
85}