Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| MediaService | |
0.00% |
0 / 29 |
|
0.00% |
0 / 8 |
240 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMediaSources | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getMediaSource | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getBrowsableSources | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getWritableSources | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| addMediaSource | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| removeMediaSource | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| registerMediaSources | |
0.00% |
0 / 3 |
|
0.00% |
0 / 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) 2015-2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\media; |
| 23 | |
| 24 | use common_Exception; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | |
| 29 | /** |
| 30 | * Service to manage the media sources |
| 31 | * |
| 32 | * To be used as if it were a singleton until serviceManager in place |
| 33 | */ |
| 34 | class MediaService extends ConfigurableService |
| 35 | { |
| 36 | public const SERVICE_ID = 'tao/MediaService'; |
| 37 | |
| 38 | public const OPTION_SOURCE = 'source'; |
| 39 | |
| 40 | /** |
| 41 | * Scheme name used to identify media resource URLs |
| 42 | */ |
| 43 | public const SCHEME_NAME = 'taomedia'; |
| 44 | |
| 45 | /** |
| 46 | * @deprecated backward compatibility |
| 47 | */ |
| 48 | public static function singleton(): self |
| 49 | { |
| 50 | return ServiceManager::getServiceManager()->get(self::SERVICE_ID); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @var array |
| 55 | */ |
| 56 | private $mediaSources; |
| 57 | |
| 58 | /** |
| 59 | * Return all configured media sources |
| 60 | * |
| 61 | * @return MediaBrowser[] |
| 62 | */ |
| 63 | protected function getMediaSources(): array |
| 64 | { |
| 65 | if (is_null($this->mediaSources)) { |
| 66 | $this->mediaSources = []; |
| 67 | foreach ($this->getOption(self::OPTION_SOURCE) as $mediaSourceId => $mediaSource) { |
| 68 | $this->mediaSources[$mediaSourceId] = $this->propagate($mediaSource); |
| 69 | } |
| 70 | } |
| 71 | return $this->mediaSources; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the media source specified by $mediaSourceId |
| 76 | * |
| 77 | * @throws common_Exception |
| 78 | */ |
| 79 | public function getMediaSource(string $mediaSourceId): MediaBrowser |
| 80 | { |
| 81 | $sources = $this->getMediaSources(); |
| 82 | if (!isset($sources[$mediaSourceId])) { |
| 83 | throw new common_Exception('Media Sources Configuration for source ' . $mediaSourceId . ' not found'); |
| 84 | } |
| 85 | return $sources[$mediaSourceId]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns all media sources that are browsable |
| 90 | * |
| 91 | * @return MediaBrowser[] |
| 92 | */ |
| 93 | public function getBrowsableSources(): array |
| 94 | { |
| 95 | $returnValue = []; |
| 96 | foreach ($this->getMediaSources() as $id => $source) { |
| 97 | if ($source instanceof MediaBrowser) { |
| 98 | $returnValue[$id] = $source; |
| 99 | } |
| 100 | } |
| 101 | return $returnValue; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns all media sources that can write |
| 106 | * |
| 107 | * @return MediaManagement[] |
| 108 | */ |
| 109 | public function getWritableSources(): array |
| 110 | { |
| 111 | $returnValue = []; |
| 112 | foreach ($this->getMediaSources() as $id => $source) { |
| 113 | if ($source instanceof MediaManagement) { |
| 114 | $returnValue[$id] = $source; |
| 115 | } |
| 116 | } |
| 117 | return $returnValue; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Adds a media source to Tao |
| 122 | * |
| 123 | * WARNING: Will always add the mediasource as 'mediamanager' as other |
| 124 | * identifiers are not supported by js widget |
| 125 | */ |
| 126 | public function addMediaSource(MediaBrowser $source): bool |
| 127 | { |
| 128 | // ensure loaded |
| 129 | $mediaSources = $this->getMediaSources(); |
| 130 | // only mediaSource called 'mediamanager' supported |
| 131 | $mediaSources['mediamanager'] = $source; |
| 132 | return $this->registerMediaSources($mediaSources); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Removes a media source for tao |
| 137 | */ |
| 138 | public function removeMediaSource(string $sourceId): bool |
| 139 | { |
| 140 | // ensure loaded |
| 141 | $mediaSources = $this->getMediaSources(); |
| 142 | unset($mediaSources[$sourceId]); |
| 143 | return $this->registerMediaSources($mediaSources); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @throws InvalidServiceManagerException |
| 148 | * @throws common_Exception |
| 149 | */ |
| 150 | public function registerMediaSources($sources): bool |
| 151 | { |
| 152 | $this->setOption(self::OPTION_SOURCE, $sources); |
| 153 | $this->getServiceManager()->register(self::SERVICE_ID, $this); |
| 154 | return true; |
| 155 | } |
| 156 | } |