Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaToMediaUnitProcessor
93.75% covered (success)
93.75%
15 / 16
80.00% covered (warning)
80.00%
4 / 5
9.02
0.00% covered (danger)
0.00%
0 / 1
 process
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
5.01
 getSharedStimulusExtractor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileManager
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMediaRelationUpdateService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSharedStimulusResourceSpecification
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\relation\task;
24
25use common_Exception;
26use core_kernel_classes_EmptyProperty;
27use core_kernel_classes_Resource;
28use Exception;
29use oat\generis\model\OntologyAwareTrait;
30use oat\oatbox\filesystem\File;
31use oat\oatbox\service\ConfigurableService;
32use oat\tao\model\task\migration\service\ResultUnitProcessorInterface;
33use oat\tao\model\task\migration\ResultUnit;
34use oat\taoMediaManager\model\fileManagement\FileManagement;
35use oat\taoMediaManager\model\MediaService;
36use oat\taoMediaManager\model\relation\service\update\MediaRelationUpdateService;
37use oat\taoMediaManager\model\sharedStimulus\parser\SharedStimulusMediaExtractor;
38use oat\taoMediaManager\model\sharedStimulus\specification\SharedStimulusResourceSpecification;
39use oat\taoMediaManager\model\TaoMediaOntology;
40
41class MediaToMediaUnitProcessor extends ConfigurableService implements ResultUnitProcessorInterface
42{
43    use OntologyAwareTrait;
44
45    /**
46     * @throws common_Exception
47     * @throws core_kernel_classes_EmptyProperty
48     */
49    public function process(ResultUnit $unit): void
50    {
51        $resource = $unit->getResult();
52
53        if (!$resource instanceof core_kernel_classes_Resource) {
54            throw new Exception('Unit is not a resource');
55        }
56
57        if ($this->getSharedStimulusResourceSpecification()->isSatisfiedBy($resource)) {
58            $fileLink = $resource->getUniquePropertyValue(
59                $this->getProperty(TaoMediaOntology::PROPERTY_LINK)
60            );
61
62            $fileLink = $fileLink instanceof core_kernel_classes_Resource ? $fileLink->getUri() : (string)$fileLink;
63            $fileSource = $this->getFileManager()->getFileStream($fileLink);
64            $content = $fileSource instanceof File ? $fileSource->read() : $fileSource->getContents();
65
66            $elementIds = $this->getSharedStimulusExtractor()->extractMediaIdentifiers($content);
67            $this->getMediaRelationUpdateService()->updateByTargetId($resource->getUri(), $elementIds);
68        }
69    }
70
71    private function getSharedStimulusExtractor(): SharedStimulusMediaExtractor
72    {
73        return $this->getServiceLocator()->get(SharedStimulusMediaExtractor::class);
74    }
75
76    private function getFileManager(): FileManagement
77    {
78        return $this->getServiceLocator()->get(FileManagement::SERVICE_ID);
79    }
80
81    private function getMediaRelationUpdateService(): MediaRelationUpdateService
82    {
83        return $this->getServiceLocator()->get(MediaRelationUpdateService::class);
84    }
85
86    private function getSharedStimulusResourceSpecification(): SharedStimulusResourceSpecification
87    {
88        return $this->getServiceLocator()->get(SharedStimulusResourceSpecification::class);
89    }
90}