Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SharedStimulusMediaParser
96.43% covered (success)
96.43%
27 / 28
83.33% covered (warning)
83.33%
5 / 6
13
0.00% covered (danger)
0.00%
0 / 1
 extractMedia
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 withMediaResolver
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 processImages
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 processVideos
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 processMediaSource
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 getMediaResolver
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
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\parser;
24
25use oat\tao\helpers\Base64;
26use oat\oatbox\service\ConfigurableService;
27use oat\tao\model\media\TaoMediaException;
28use oat\tao\model\media\TaoMediaResolver;
29use oat\taoMediaManager\model\MediaSource;
30use qtism\data\storage\xml\XmlDocument;
31use qtism\data\storage\xml\XmlStorageException;
32
33class SharedStimulusMediaParser extends ConfigurableService
34{
35    /** @var TaoMediaResolver */
36    private $mediaResolver;
37
38    public function extractMedia(string $xml, callable $processImageIdentifier): array
39    {
40        try {
41            $xmlDocument = new XmlDocument();
42            $xmlDocument->loadFromString($xml, false);
43        } catch (XmlStorageException $e) {
44            throw new TaoMediaException('Shared stimulus XML cannot be processed.', 0, $e);
45        }
46
47        return array_merge(
48            $this->processImages($xmlDocument, $processImageIdentifier),
49            $this->processVideos($xmlDocument, $processImageIdentifier)
50        );
51    }
52
53    public function withMediaResolver(TaoMediaResolver $resolver)
54    {
55        $this->mediaResolver = $resolver;
56
57        return $this;
58    }
59
60    private function processImages(XmlDocument $xmlDocument, callable $processImageIdentifier): array
61    {
62        $mediaImages = [];
63        $images = $xmlDocument->getDocumentComponent()->getComponentsByClassName('img');
64
65        foreach ($images as $image) {
66            $this->processMediaSource($image->getSrc(), $processImageIdentifier, $mediaImages);
67        }
68
69        return $mediaImages;
70    }
71
72    private function processVideos(XmlDocument $xmlDocument, callable $processImageIdentifier): array
73    {
74        $mediaVideos = [];
75        $videos = $xmlDocument->getDocumentComponent()->getComponentsByClassName('object');
76
77        foreach ($videos as $video) {
78            $this->processMediaSource($video->getData(), $processImageIdentifier, $mediaVideos);
79        }
80
81        return $mediaVideos;
82    }
83
84    private function processMediaSource(string $uri, callable $processor, array &$matches): void
85    {
86        if (!Base64::isEncoded($uri)) {
87            if (isset(parse_url($uri)['scheme'])) {
88                $asset = $this->getMediaResolver()->resolve($uri);
89
90                if ($asset->getMediaSource() instanceof MediaSource) {
91                    $matches[] = $processor($asset);
92                }
93            }
94        }
95    }
96
97    private function getMediaResolver(): TaoMediaResolver
98    {
99        if (!$this->mediaResolver) {
100            $this->mediaResolver = new TaoMediaResolver();
101        }
102
103        return $this->mediaResolver;
104    }
105}