Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
SharedStimulusRepository | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
find | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
getContent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getPropertyValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getFileManagement | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOntology | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileSourceUnserializer | |
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) 2020 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoMediaManager\model\sharedStimulus\repository; |
24 | |
25 | use common_Exception; |
26 | use core_kernel_classes_EmptyProperty; |
27 | use core_kernel_classes_Literal; |
28 | use core_kernel_classes_Resource; |
29 | use oat\generis\model\data\Ontology; |
30 | use oat\oatbox\service\ConfigurableService; |
31 | use oat\taoMediaManager\model\fileManagement\FileManagement; |
32 | use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer; |
33 | use oat\taoMediaManager\model\sharedStimulus\FindQuery; |
34 | use oat\taoMediaManager\model\sharedStimulus\SharedStimulus; |
35 | use oat\taoMediaManager\model\TaoMediaOntology; |
36 | |
37 | class SharedStimulusRepository extends ConfigurableService implements SharedStimulusRepositoryInterface |
38 | { |
39 | /** |
40 | * @throws common_Exception |
41 | * @throws core_kernel_classes_EmptyProperty |
42 | */ |
43 | public function find(FindQuery $query): SharedStimulus |
44 | { |
45 | $resource = $this->getOntology()->getResource($query->getId()); |
46 | |
47 | return new SharedStimulus( |
48 | $query->getId(), |
49 | $this->getPropertyValue($resource, TaoMediaOntology::PROPERTY_ALT_TEXT), |
50 | $this->getPropertyValue($resource, TaoMediaOntology::PROPERTY_LANGUAGE), |
51 | $this->getContent($resource) |
52 | ); |
53 | } |
54 | |
55 | /** |
56 | * @throws common_Exception |
57 | * @throws core_kernel_classes_EmptyProperty |
58 | */ |
59 | private function getContent(core_kernel_classes_Resource $resource): string |
60 | { |
61 | $link = $this->getPropertyValue( |
62 | $resource, |
63 | TaoMediaOntology::PROPERTY_LINK |
64 | ); |
65 | |
66 | $link = $this->getFileSourceUnserializer()->unserialize($link); |
67 | |
68 | return (string)$this->getFileManagement()->getFileStream($link); |
69 | } |
70 | |
71 | /** |
72 | * @throws common_Exception |
73 | * @throws core_kernel_classes_EmptyProperty |
74 | */ |
75 | private function getPropertyValue(core_kernel_classes_Resource $resource, string $uri) |
76 | { |
77 | $propertyValue = $resource->getUniquePropertyValue($resource->getProperty($uri)); |
78 | |
79 | if ($propertyValue instanceof core_kernel_classes_Resource) { |
80 | return $propertyValue->getUri(); |
81 | } |
82 | |
83 | if ($propertyValue instanceof core_kernel_classes_Literal) { |
84 | return (string)$propertyValue; |
85 | } |
86 | } |
87 | |
88 | private function getFileManagement(): FileManagement |
89 | { |
90 | return $this->getServiceLocator()->get(FileManagement::SERVICE_ID); |
91 | } |
92 | |
93 | private function getOntology(): Ontology |
94 | { |
95 | return $this->getServiceLocator()->get(Ontology::SERVICE_ID); |
96 | } |
97 | |
98 | private function getFileSourceUnserializer(): FileSourceUnserializer |
99 | { |
100 | return $this->getServiceLocator()->get(FileSourceUnserializer::class); |
101 | } |
102 | } |