Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UploadStylesheetService | |
0.00% |
0 / 28 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| save | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
12 | |||
| getStylesheetRepository | |
0.00% |
0 / 1 |
|
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\sharedStimulus\css\service; |
| 24 | |
| 25 | use oat\taoMediaManager\model\sharedStimulus\css\dto\UploadedStylesheet; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository; |
| 28 | |
| 29 | class UploadStylesheetService extends ConfigurableService |
| 30 | { |
| 31 | public function save(UploadedStylesheet $uploadedStylesheetDTO): array |
| 32 | { |
| 33 | $passagePath = $this->getStylesheetRepository()->getPath($uploadedStylesheetDTO->getUri()); |
| 34 | $link = $passagePath |
| 35 | . DIRECTORY_SEPARATOR |
| 36 | . StylesheetRepository::STYLESHEETS_DIRECTORY |
| 37 | . DIRECTORY_SEPARATOR |
| 38 | . $uploadedStylesheetDTO->getFileName(); |
| 39 | |
| 40 | $tmpResource = $uploadedStylesheetDTO->getFileResource(); |
| 41 | $size = filesize($uploadedStylesheetDTO->getTmpFileLink()); |
| 42 | $this->getStylesheetRepository()->writeStream($link, $tmpResource); |
| 43 | |
| 44 | if (is_resource($tmpResource)) { |
| 45 | fclose($tmpResource); |
| 46 | } else { |
| 47 | $this->logWarning( |
| 48 | sprintf( |
| 49 | 'Stream for asset stylesheet file "%s" is not valid. It may be already closed', |
| 50 | $uploadedStylesheetDTO->getFileName() |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | if (is_writable($uploadedStylesheetDTO->getTmpFileLink())) { |
| 56 | unlink($uploadedStylesheetDTO->getTmpFileLink()); |
| 57 | } |
| 58 | |
| 59 | return [ |
| 60 | 'alt' => $uploadedStylesheetDTO->getFileName(), |
| 61 | 'link' => DIRECTORY_SEPARATOR . $uploadedStylesheetDTO->getFileName(), |
| 62 | 'mime' => 'text/css', |
| 63 | 'name' => $uploadedStylesheetDTO->getFileName(), |
| 64 | 'size' => $size, |
| 65 | 'uri' => DIRECTORY_SEPARATOR . $uploadedStylesheetDTO->getFileName() |
| 66 | ]; |
| 67 | } |
| 68 | |
| 69 | private function getStylesheetRepository(): StylesheetRepository |
| 70 | { |
| 71 | return $this->getServiceLocator()->get(StylesheetRepository::class); |
| 72 | } |
| 73 | } |