Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemContentCopier
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 copy
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
 copyItemModel
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
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 * @author Andrei Shapiro <andrei.shapiro@taotesting.com>
21 */
22
23declare(strict_types=1);
24
25namespace oat\taoItems\model\Copier;
26
27use core_kernel_classes_Resource;
28use oat\oatbox\event\EventManager;
29use oat\oatbox\filesystem\Directory;
30use oat\taoItems\model\TaoItemOntology;
31use taoItems_models_classes_ItemsService;
32use oat\taoItems\model\event\ItemContentClonedEvent;
33use oat\generis\model\fileReference\FileReferenceSerializer;
34use oat\tao\model\resources\Contract\InstanceContentCopierInterface;
35
36class ItemContentCopier implements InstanceContentCopierInterface
37{
38    /** @var FileReferenceSerializer */
39    private $fileReferenceSerializer;
40
41    /** @var taoItems_models_classes_ItemsService */
42    private $itemsService;
43
44    /** @var EventManager */
45    private $eventManager;
46
47    public function __construct(
48        FileReferenceSerializer $fileReferenceSerializer,
49        taoItems_models_classes_ItemsService $itemsService,
50        EventManager $eventManager
51    ) {
52        $this->fileReferenceSerializer = $fileReferenceSerializer;
53        $this->itemsService = $itemsService;
54        $this->eventManager = $eventManager;
55    }
56
57    public function copy(
58        core_kernel_classes_Resource $instance,
59        core_kernel_classes_Resource $destinationInstance
60    ): void {
61        $this->copyItemModel($instance, $destinationInstance);
62        $property = $instance->getProperty(TaoItemOntology::PROPERTY_ITEM_CONTENT);
63
64        foreach ($instance->getUsedLanguages($property) as $lang) {
65            $sourceItemDirectory = $this->itemsService->getItemDirectory($instance, $lang);
66            $destinationItemDirectory = $this->itemsService->getItemDirectory($destinationInstance, $lang);
67            $propertyValues = $instance->getPropertyValuesCollection($property, ['lg' => $lang]);
68
69            foreach ($propertyValues as $propertyValue) {
70                $id = $propertyValue instanceof core_kernel_classes_Resource
71                    ? $propertyValue->getUri()
72                    : (string) $propertyValue;
73
74                $sourceDirectory = $this->fileReferenceSerializer->unserializeDirectory($id);
75                $iterator = $sourceDirectory->getFlyIterator(
76                    Directory::ITERATOR_FILE | Directory::ITERATOR_RECURSIVE
77                );
78
79                foreach ($iterator as $iteratorFile) {
80                    $newFile = $destinationItemDirectory->getFile($sourceItemDirectory->getRelPath($iteratorFile));
81                    $newFile->write($iteratorFile->readStream());
82                }
83
84                $destinationDirectory = $destinationItemDirectory->getDirectory(
85                    $sourceItemDirectory->getRelPath($sourceDirectory)
86                );
87                $this->fileReferenceSerializer->serialize($destinationDirectory);
88            }
89        }
90
91        $this->eventManager->trigger(
92            new ItemContentClonedEvent($instance->getUri(), $destinationInstance->getUri())
93        );
94    }
95
96    private function copyItemModel(
97        core_kernel_classes_Resource $instance,
98        core_kernel_classes_Resource $destinationInstance
99    ): void {
100        $itemModelProperty = $instance->getProperty(TaoItemOntology::PROPERTY_ITEM_MODEL);
101        $model = $instance->getOnePropertyValue($itemModelProperty);
102
103        $destinationInstance->editPropertyValues(
104            $itemModelProperty,
105            $model instanceof core_kernel_classes_Resource ? $model : null
106        );
107    }
108}