Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.74% |
35 / 39 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InstanceMetadataCopier | |
89.74% |
35 / 39 |
|
57.14% |
4 / 7 |
18.35 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| addPropertyUriToBlacklist | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| addPropertyUrisToBlacklist | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| copy | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
6.02 | |||
| getOriginalProperty | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| isFileProperty | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| copyFile | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\resources\Service; |
| 26 | |
| 27 | use helpers_File; |
| 28 | use core_kernel_classes_Literal; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use oat\generis\model\GenerisRdf; |
| 31 | use core_kernel_classes_Property; |
| 32 | use oat\generis\model\OntologyRdf; |
| 33 | use oat\oatbox\filesystem\FileSystemService; |
| 34 | use oat\generis\model\fileReference\FileReferenceSerializer; |
| 35 | use oat\tao\model\resources\Contract\ClassMetadataMapperInterface; |
| 36 | use oat\tao\model\resources\Contract\InstanceMetadataCopierInterface; |
| 37 | |
| 38 | class InstanceMetadataCopier implements InstanceMetadataCopierInterface |
| 39 | { |
| 40 | /** @var ClassMetadataMapperInterface */ |
| 41 | private $classMetadataMapper; |
| 42 | |
| 43 | /** @var FileReferenceSerializer */ |
| 44 | private $fileReferenceSerializer; |
| 45 | |
| 46 | /** @var FileSystemService */ |
| 47 | private $fileSystemService; |
| 48 | |
| 49 | /** @var string[] */ |
| 50 | private $blacklistedProperties = [ |
| 51 | OntologyRdf::RDF_TYPE, |
| 52 | ]; |
| 53 | |
| 54 | /** @var array<string, bool> */ |
| 55 | private $fileProperties = []; |
| 56 | |
| 57 | private array $allowedNonCustomerProperties; |
| 58 | |
| 59 | public function __construct( |
| 60 | ClassMetadataMapperInterface $classMetadataMapper, |
| 61 | FileReferenceSerializer $fileReferenceSerializer, |
| 62 | FileSystemService $fileSystemService, |
| 63 | array $allowedNonCustomerProperties = [] |
| 64 | ) { |
| 65 | $this->classMetadataMapper = $classMetadataMapper; |
| 66 | $this->fileReferenceSerializer = $fileReferenceSerializer; |
| 67 | $this->fileSystemService = $fileSystemService; |
| 68 | $this->allowedNonCustomerProperties = $allowedNonCustomerProperties; |
| 69 | } |
| 70 | |
| 71 | public function addPropertyUriToBlacklist(string $propertyUri): void |
| 72 | { |
| 73 | if (!in_array($propertyUri, $this->blacklistedProperties, true)) { |
| 74 | $this->blacklistedProperties[] = $propertyUri; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public function addPropertyUrisToBlacklist(array $propertyUris): void |
| 79 | { |
| 80 | $this->blacklistedProperties = array_merge($this->blacklistedProperties, $propertyUris); |
| 81 | } |
| 82 | |
| 83 | public function copy( |
| 84 | core_kernel_classes_Resource $instance, |
| 85 | core_kernel_classes_Resource $destinationInstance |
| 86 | ): void { |
| 87 | $destinationClass = current($destinationInstance->getTypes()); |
| 88 | |
| 89 | foreach ($destinationClass->getProperties(true) as $destinationProperty) { |
| 90 | $originalProperty = $this->getOriginalProperty($destinationProperty); |
| 91 | |
| 92 | if ( |
| 93 | $originalProperty === null |
| 94 | || in_array($originalProperty->getUri(), $this->blacklistedProperties, true) |
| 95 | ) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $propertyValues = $instance->getPropertyValuesCollection($originalProperty); |
| 100 | $isFileProperty = $this->isFileProperty($originalProperty); |
| 101 | |
| 102 | /** @var core_kernel_classes_Literal|core_kernel_classes_Resource|core_kernel_classes_Resource[] $propertyValue */ |
| 103 | foreach ($propertyValues->getIterator() as $propertyValue) { |
| 104 | if ($isFileProperty) { |
| 105 | $this->copyFile($destinationInstance, $destinationProperty, $propertyValue); |
| 106 | |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | $destinationInstance->setPropertyValue($destinationProperty, $propertyValue); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private function getOriginalProperty(core_kernel_classes_Property $property): ?core_kernel_classes_Property |
| 116 | { |
| 117 | $originalPropertyUri = $this->classMetadataMapper->get($property); |
| 118 | |
| 119 | if ($originalPropertyUri === null) { |
| 120 | return $property->isCustom() || in_array($property->getUri(), $this->allowedNonCustomerProperties) ? |
| 121 | $property : |
| 122 | null; |
| 123 | } |
| 124 | |
| 125 | return $property->getProperty($originalPropertyUri); |
| 126 | } |
| 127 | |
| 128 | private function isFileProperty(core_kernel_classes_Property $property): bool |
| 129 | { |
| 130 | $propertyUri = $property->getUri(); |
| 131 | |
| 132 | if (!array_key_exists($propertyUri, $this->fileProperties)) { |
| 133 | $range = $property->getRange(); |
| 134 | |
| 135 | $this->fileProperties[$propertyUri] = $range !== null |
| 136 | && $range->getUri() === GenerisRdf::CLASS_GENERIS_FILE; |
| 137 | } |
| 138 | |
| 139 | return $this->fileProperties[$propertyUri]; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param core_kernel_classes_Literal|core_kernel_classes_Resource $propertyValue |
| 144 | */ |
| 145 | private function copyFile( |
| 146 | core_kernel_classes_Resource $instance, |
| 147 | core_kernel_classes_Property $property, |
| 148 | $propertyValue |
| 149 | ): void { |
| 150 | $oldFile = $this->fileReferenceSerializer->unserializeFile($propertyValue->getUri()); |
| 151 | |
| 152 | $newFile = $this->fileSystemService |
| 153 | ->getDirectory($oldFile->getFileSystemId()) |
| 154 | ->getFile(helpers_File::createFileName($oldFile->getBasename())); |
| 155 | $newFile->write($oldFile->readStream()); |
| 156 | $newFileUri = $this->fileReferenceSerializer->serialize($newFile); |
| 157 | |
| 158 | $instance->setPropertyValue($property, $instance->getResource($newFileUri)); |
| 159 | } |
| 160 | } |