Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
AssetMetadataCopier | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
copy | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
copyProperty | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
copyNonLanguageDependentProperty | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
copyLanguageDependentProperty | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
formatPropertyValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
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 | |
21 | namespace oat\taoMediaManager\model\classes\Copier; |
22 | |
23 | use oat\tao\model\resources\Contract\InstanceMetadataCopierInterface; |
24 | use oat\taoMediaManager\model\TaoMediaOntology; |
25 | use core_kernel_classes_Resource; |
26 | use core_kernel_classes_Property; |
27 | |
28 | class AssetMetadataCopier implements InstanceMetadataCopierInterface |
29 | { |
30 | /** |
31 | * MD5 hash of the file contents |
32 | */ |
33 | private const PROPERTY_MD5 = TaoMediaOntology::PROPERTY_MD5; |
34 | |
35 | private const PROPERTY_ALT_TEXT = TaoMediaOntology::PROPERTY_ALT_TEXT; |
36 | |
37 | private const PROPERTY_LANGUAGE = TaoMediaOntology::PROPERTY_LANGUAGE; |
38 | |
39 | private const PROPERTY_MIME = TaoMediaOntology::PROPERTY_MIME_TYPE; |
40 | |
41 | /** @var InstanceMetadataCopierInterface */ |
42 | private $nestedCopier; |
43 | |
44 | public function __construct(InstanceMetadataCopierInterface $nestedCopier) |
45 | { |
46 | $this->nestedCopier = $nestedCopier; |
47 | } |
48 | |
49 | public function copy( |
50 | core_kernel_classes_Resource $instance, |
51 | core_kernel_classes_Resource $destinationInstance |
52 | ): void { |
53 | $this->nestedCopier->copy($instance, $destinationInstance); |
54 | |
55 | $this->copyProperty($instance, $destinationInstance, self::PROPERTY_ALT_TEXT); |
56 | $this->copyProperty($instance, $destinationInstance, self::PROPERTY_LANGUAGE); |
57 | $this->copyProperty($instance, $destinationInstance, self::PROPERTY_MD5); |
58 | $this->copyProperty($instance, $destinationInstance, self::PROPERTY_MIME); |
59 | } |
60 | |
61 | private function copyProperty( |
62 | core_kernel_classes_Resource $source, |
63 | core_kernel_classes_Resource $destination, |
64 | string $propertyId |
65 | ): void { |
66 | $property = $source->getProperty($propertyId); |
67 | |
68 | if ($property->isLgDependent()) { |
69 | $this->copyLanguageDependentProperty($source, $destination, $property); |
70 | return; |
71 | } |
72 | |
73 | $this->copyNonLanguageDependentProperty($source, $destination, $property); |
74 | } |
75 | |
76 | private function copyNonLanguageDependentProperty( |
77 | core_kernel_classes_Resource $source, |
78 | core_kernel_classes_Resource $destination, |
79 | core_kernel_classes_Property $property |
80 | ): void { |
81 | $values = $source->getPropertyValuesCollection($property); |
82 | |
83 | foreach ($values as $value) { |
84 | $destination->setPropertyValue( |
85 | $property, |
86 | $this->formatPropertyValue($value) |
87 | ); |
88 | } |
89 | } |
90 | |
91 | private function copyLanguageDependentProperty( |
92 | core_kernel_classes_Resource $source, |
93 | core_kernel_classes_Resource $destination, |
94 | core_kernel_classes_Property $property |
95 | ): void { |
96 | foreach ($source->getUsedLanguages($property) as $lang) { |
97 | /** @var $lang string */ |
98 | |
99 | $values = $source->getPropertyValuesCollection( |
100 | $property, |
101 | [ |
102 | 'lg' => $lang |
103 | ] |
104 | ); |
105 | |
106 | foreach ($values as $value) { |
107 | $destination->setPropertyValueByLg( |
108 | $property, |
109 | $this->formatPropertyValue($value), |
110 | $lang |
111 | ); |
112 | } |
113 | } |
114 | } |
115 | |
116 | private function formatPropertyValue($value): string |
117 | { |
118 | if ($value instanceof core_kernel_classes_Resource) { |
119 | return $value->getUri(); |
120 | } |
121 | |
122 | return (string) $value; |
123 | } |
124 | } |