Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
27 / 36 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AssetStylesheetLoader | |
75.00% |
27 / 36 |
|
80.00% |
4 / 5 |
11.56 | |
0.00% |
0 / 1 |
| loadAssetsFromAssetResource | |
64.00% |
16 / 25 |
|
0.00% |
0 / 1 |
7.68 | |||
| buildAssetPathFromPropertyName | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getFileSystem | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getFileSystemService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFlySystemManagement | |
100.00% |
1 / 1 |
|
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\Export\Stylesheet; |
| 24 | |
| 25 | use League\Flysystem\StorageAttributes; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\oatbox\filesystem\FilesystemException; |
| 28 | use oat\oatbox\filesystem\FilesystemInterface; |
| 29 | use oat\oatbox\filesystem\FileSystemService; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | |
| 32 | /** @todo fix the implementation as taoQtiItem MUST NOT depend on taoMediaManager */ |
| 33 | use oat\taoMediaManager\model\fileManagement\FlySystemManagement; |
| 34 | use oat\taoQtiItem\model\Export\AbstractQTIItemExporter; |
| 35 | use tao_helpers_Uri as UriHelper; |
| 36 | class AssetStylesheetLoader extends ConfigurableService |
| 37 | { |
| 38 | use OntologyAwareTrait; |
| 39 | |
| 40 | public const ASSET_CSS_DIRECTORY_NAME = 'css'; |
| 41 | |
| 42 | public function loadAssetsFromAssetResource(string $link): ?array |
| 43 | { |
| 44 | // fetch multiple |
| 45 | $asset = $this->getResource(UriHelper::decode($link)); |
| 46 | |
| 47 | if ($asset->exists()) { |
| 48 | $property = (string)$asset->getUniquePropertyValue( |
| 49 | $this->getProperty(AbstractQTIItemExporter::PROPERTY_LINK) |
| 50 | ); |
| 51 | |
| 52 | $stylesheetPath = $this->buildAssetPathFromPropertyName($property); |
| 53 | try { |
| 54 | $cssFiles = $this->getFileSystem()->listContents($stylesheetPath)->toArray(); |
| 55 | $cssFilesInfo = []; |
| 56 | |
| 57 | foreach ($cssFiles as $key => $file) { |
| 58 | if ($file['type'] == 'file') { |
| 59 | $cssFilesInfo[$key] = $file instanceof StorageAttributes ? $file->jsonSerialize() : $file; |
| 60 | $cssFilesInfo[$key]['stream'] = $this->getFileSystem()->readStream( |
| 61 | $stylesheetPath . DIRECTORY_SEPARATOR . basename($file['path']) |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $cssFilesInfo; |
| 67 | } catch (FilesystemException $exception) { |
| 68 | $this->getLogger()->error( |
| 69 | 'Error loading asset stylesheet', |
| 70 | [ |
| 71 | 'exception' => $exception, |
| 72 | 'path' => $stylesheetPath, |
| 73 | ] |
| 74 | ); |
| 75 | |
| 76 | throw $exception; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | private function buildAssetPathFromPropertyName(string $property) |
| 84 | { |
| 85 | return implode( |
| 86 | DIRECTORY_SEPARATOR, |
| 87 | [ |
| 88 | dirname($property), |
| 89 | self::ASSET_CSS_DIRECTORY_NAME, |
| 90 | ] |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | private function getFileSystem(): FilesystemInterface |
| 95 | { |
| 96 | return $this->getFileSystemService() |
| 97 | ->getFileSystem($this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS)); |
| 98 | } |
| 99 | |
| 100 | private function getFileSystemService(): FileSystemService |
| 101 | { |
| 102 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 103 | } |
| 104 | |
| 105 | private function getFlySystemManagement(): FlySystemManagement |
| 106 | { |
| 107 | // FIXME this violates the order of dependencies. |
| 108 | // taoQtiItem cannot depend on taoMediaManager as it causes a circular dependency |
| 109 | // caused by [#1766](https://github.com/oat-sa/extension-tao-itemqti/pull/1766) |
| 110 | return $this->getServiceLocator()->get(FlySystemManagement::SERVICE_ID); |
| 111 | } |
| 112 | } |