Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ClassMetadataCopier | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
6.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| copy | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
| copyProperty | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| skipAlias | |
100.00% |
1 / 1 |
|
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 core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Property; |
| 29 | use oat\generis\model\GenerisRdf; |
| 30 | use oat\generis\model\OntologyRdfs; |
| 31 | use oat\tao\model\resources\Contract\ClassMetadataCopierInterface; |
| 32 | use oat\tao\model\resources\Contract\ClassMetadataMapperInterface; |
| 33 | |
| 34 | class ClassMetadataCopier implements ClassMetadataCopierInterface |
| 35 | { |
| 36 | /** @var ClassMetadataMapperInterface */ |
| 37 | private $classMetadataMapper; |
| 38 | |
| 39 | /** @var string[] */ |
| 40 | private $copiedProperties = []; |
| 41 | |
| 42 | public function __construct(ClassMetadataMapperInterface $classMetadataMapper) |
| 43 | { |
| 44 | $this->classMetadataMapper = $classMetadataMapper; |
| 45 | } |
| 46 | |
| 47 | public function copy(core_kernel_classes_Class $class, core_kernel_classes_Class $destinationClass): void |
| 48 | { |
| 49 | $allClassProperties = $class->getProperties(true); |
| 50 | $destinationClassProperties = $destinationClass->getProperties(true); |
| 51 | $properties = array_diff_key($allClassProperties, $destinationClassProperties); |
| 52 | |
| 53 | foreach ($properties as $propertyUri => $property) { |
| 54 | if (in_array($propertyUri, $this->copiedProperties, true)) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | $newProperty = $this->copyProperty($property, $destinationClass); |
| 59 | |
| 60 | $this->copiedProperties[] = $propertyUri; |
| 61 | $this->classMetadataMapper->add($property, $newProperty); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private function copyProperty( |
| 66 | core_kernel_classes_Property $property, |
| 67 | core_kernel_classes_Class $destinationClass |
| 68 | ): core_kernel_classes_Property { |
| 69 | $newPropertyResource = $property->duplicate(); |
| 70 | $newProperty = $newPropertyResource->getProperty($newPropertyResource->getUri()); |
| 71 | |
| 72 | $newProperty->removePropertyValues($newProperty->getProperty(OntologyRdfs::RDFS_DOMAIN)); |
| 73 | $newProperty->setDomain($destinationClass); |
| 74 | |
| 75 | $this->skipAlias($newProperty); |
| 76 | |
| 77 | return $newProperty; |
| 78 | } |
| 79 | |
| 80 | private function skipAlias(core_kernel_classes_Property $newProperty) |
| 81 | { |
| 82 | $newProperty->removePropertyValues($newProperty->getProperty(GenerisRdf::PROPERTY_ALIAS)); |
| 83 | } |
| 84 | } |