Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
SharedStimulusRepository
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 find
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getContent
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getPropertyValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getFileManagement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOntology
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileSourceUnserializer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\model\sharedStimulus\repository;
24
25use common_Exception;
26use core_kernel_classes_EmptyProperty;
27use core_kernel_classes_Literal;
28use core_kernel_classes_Resource;
29use oat\generis\model\data\Ontology;
30use oat\oatbox\service\ConfigurableService;
31use oat\taoMediaManager\model\fileManagement\FileManagement;
32use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer;
33use oat\taoMediaManager\model\sharedStimulus\FindQuery;
34use oat\taoMediaManager\model\sharedStimulus\SharedStimulus;
35use oat\taoMediaManager\model\TaoMediaOntology;
36
37class 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}