Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
InlineSource
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
9 / 9
12
100.00% covered (success)
100.00%
1 / 1
 getDirectories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDirectory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 download
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFileStream
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBaseName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 detectFileExtension
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 createResource
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 verifyDataUrlScheme
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
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) 2021 (original work) Open Assessment Technologies SA ;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\media\sourceStrategy;
24
25use common_Exception;
26use GuzzleHttp\Psr7\Stream;
27use League\MimeTypeDetection\GeneratedExtensionToMimeTypeMap;
28use oat\tao\model\media\MediaBrowser;
29use oat\tao\model\media\mediaSource\DirectorySearchQuery;
30use RuntimeException;
31
32class InlineSource implements MediaBrowser
33{
34    public const FILENAME_PREFIX = 'inline-media-';
35
36    public function getDirectories(DirectorySearchQuery $params): array
37    {
38        throw new common_Exception(__FUNCTION__ . ' not implemented');
39    }
40
41    public function getDirectory($parentLink = '/', $acceptableMime = [], $depth = 1)
42    {
43        throw new common_Exception(__FUNCTION__ . ' not implemented');
44    }
45
46    public function getFileInfo($link)
47    {
48        throw new common_Exception(__FUNCTION__ . ' not implemented');
49    }
50
51    public function download($link)
52    {
53        $stream = $this->getFileStream($link);
54
55        return $stream->getContents();
56    }
57
58    public function getFileStream($link)
59    {
60        return new Stream($this->createResource($link));
61    }
62
63    public function getBaseName($link)
64    {
65        return self::FILENAME_PREFIX . md5($link) . '.' . $this->detectFileExtension($link);
66    }
67
68    private function detectFileExtension($link): ?string
69    {
70        $stream = $this->getFileStream($link);
71        $mimetype = $stream->getMetadata('mediatype');
72        $extension = array_search($mimetype, GeneratedExtensionToMimeTypeMap::MIME_TYPES_FOR_EXTENSIONS);
73        if (!$extension) {
74            throw new RuntimeException(
75                sprintf('Could not determined inline asset file extension from the mime type: %s.', $mimetype)
76            );
77        }
78
79        return $extension;
80    }
81
82    private function createResource($link)
83    {
84        $this->verifyDataUrlScheme($link);
85
86        $resource = @fopen($link, 'r');
87        if (!$resource) {
88            throw new RuntimeException('Invalid Data URL, could not create a resource.');
89        }
90
91        return $resource;
92    }
93
94    private function verifyDataUrlScheme($link): void
95    {
96        $urlScheme = parse_url($link, PHP_URL_SCHEME);
97        if ($urlScheme !== 'data') {
98            throw new RuntimeException('Provided URL should match the Data (RFC 2397) scheme.');
99        }
100    }
101}