Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
51.35% |
19 / 37 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| StoreService | |
51.35% |
19 / 37 |
|
50.00% |
3 / 6 |
24.93 | |
0.00% |
0 / 1 |
| store | |
54.55% |
6 / 11 |
|
0.00% |
0 / 1 |
2.38 | |||
| storeStream | |
42.86% |
9 / 21 |
|
0.00% |
0 / 1 |
9.66 | |||
| getUniqueName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\sharedStimulus\service; |
| 24 | |
| 25 | use oat\oatbox\filesystem\File; |
| 26 | use oat\oatbox\filesystem\FileSystem; |
| 27 | use oat\oatbox\filesystem\FilesystemInterface; |
| 28 | use oat\oatbox\filesystem\FileSystemService; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\taoMediaManager\model\fileManagement\FlySystemManagement; |
| 31 | |
| 32 | class StoreService extends ConfigurableService |
| 33 | { |
| 34 | /** |
| 35 | * Name of subdirectory to store stylesheets |
| 36 | */ |
| 37 | public const CSS_DIR_NAME = 'css'; |
| 38 | |
| 39 | /** |
| 40 | * @param string|File $stimulusXmlSourceFile |
| 41 | */ |
| 42 | public function store( |
| 43 | $stimulusXmlSourceFile, |
| 44 | string $stimulusFilename, |
| 45 | array $cssFiles = [] |
| 46 | ): string { |
| 47 | if ($stimulusXmlSourceFile instanceof File) { |
| 48 | return $this->storeStream( |
| 49 | $stimulusXmlSourceFile->readStream(), |
| 50 | $stimulusFilename, |
| 51 | $cssFiles |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | return $this->storeStream( |
| 56 | fopen($stimulusXmlSourceFile, 'r'), |
| 57 | $stimulusFilename, |
| 58 | $cssFiles |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param resource $stimulusXmlStream |
| 64 | */ |
| 65 | public function storeStream( |
| 66 | $stimulusXmlStream, |
| 67 | string $stimulusFilename, |
| 68 | array $cssFiles = [] |
| 69 | ): string { |
| 70 | $fs = $this->getFileSystem(); |
| 71 | |
| 72 | $dirname = $this->getUniqueName($stimulusFilename); |
| 73 | $fs->createDirectory($dirname); |
| 74 | |
| 75 | $fs->writeStream( |
| 76 | $dirname . DIRECTORY_SEPARATOR . $stimulusFilename, |
| 77 | $stimulusXmlStream |
| 78 | ); |
| 79 | |
| 80 | if (count($cssFiles)) { |
| 81 | $fs->createDirectory($dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME); |
| 82 | foreach ($cssFiles as $file) { |
| 83 | if (!file_exists($file)) { |
| 84 | $this->getLogger()->notice(sprintf("file %s does not exist", $file)); |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if (!is_readable($file)) { |
| 89 | $this->getLogger()->notice(sprintf("file %s is not readable", $file)); |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | $fs->writeStream( |
| 94 | $dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME . DIRECTORY_SEPARATOR . basename($file), |
| 95 | fopen($file, 'r') |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $dirname; |
| 101 | } |
| 102 | |
| 103 | protected function getUniqueName(string $name): string |
| 104 | { |
| 105 | return uniqid(hash('crc32', $name)); |
| 106 | } |
| 107 | |
| 108 | private function getFileSystem(): FilesystemInterface |
| 109 | { |
| 110 | return $this->getFileSystemService() |
| 111 | ->getFileSystem($this->getFlySystemManagement()->getOption(FlySystemManagement::OPTION_FS)); |
| 112 | } |
| 113 | |
| 114 | private function getFileSystemService(): FileSystemService |
| 115 | { |
| 116 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 117 | } |
| 118 | |
| 119 | private function getFlySystemManagement(): FlySystemManagement |
| 120 | { |
| 121 | return $this->getServiceLocator()->get(FlySystemManagement::SERVICE_ID); |
| 122 | } |
| 123 | } |