Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceTransferProxy
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 transfer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTransfer
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
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) 2023 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\resources\Service;
24
25use InvalidArgumentException;
26use oat\generis\model\data\Ontology;
27use oat\tao\model\resources\Command\ResourceTransferCommand;
28use oat\tao\model\resources\Contract\ResourceTransferInterface;
29use oat\tao\model\resources\ResourceTransferResult;
30
31class ResourceTransferProxy implements ResourceTransferInterface
32{
33    private ResourceTransferInterface $classCopier;
34    private ResourceTransferInterface $instanceCopier;
35    private ResourceTransferInterface $classMover;
36    private ResourceTransferInterface $instanceMover;
37    private Ontology $ontology;
38
39    public function __construct(
40        ResourceTransferInterface $classCopier,
41        ResourceTransferInterface $instanceCopier,
42        ResourceTransferInterface $classMover,
43        ResourceTransferInterface $instanceMover,
44        Ontology $ontology
45    ) {
46        $this->classCopier = $classCopier;
47        $this->instanceCopier = $instanceCopier;
48        $this->classMover = $classMover;
49        $this->instanceMover = $instanceMover;
50        $this->ontology = $ontology;
51    }
52
53    public function transfer(ResourceTransferCommand $command): ResourceTransferResult
54    {
55        return $this->getTransfer($command)->transfer($command);
56    }
57
58    private function getTransfer(ResourceTransferCommand $command): ResourceTransferInterface
59    {
60        $from = $this->ontology->getResource($command->getFrom());
61        $to = $this->ontology->getResource($command->getTo());
62
63        if (!$to->isClass()) {
64            throw new InvalidArgumentException(
65                sprintf(
66                    'The destination resource [%s:%s] is not a class',
67                    $command->getTo(),
68                    $to->getLabel()
69                )
70            );
71        }
72
73        if ($from->isClass()) {
74            if ($command->isCopyTo()) {
75                return $this->classCopier;
76            }
77
78            return $this->classMover;
79        }
80
81        if ($command->isCopyTo()) {
82            return $this->instanceCopier;
83        }
84
85        return $this->instanceMover;
86    }
87}