Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UploadStylesheetRequestHandler | |
0.00% |
0 / 10 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUploadedFileInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateUploadedFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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\css\handler; |
| 24 | |
| 25 | use common_exception_Error; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\helpers\FileUploadException; |
| 28 | use oat\taoMediaManager\model\sharedStimulus\css\dto\UploadedStylesheet; |
| 29 | use Psr\Http\Message\ServerRequestInterface; |
| 30 | use oat\taoMediaManager\model\validation\RequestValidator; |
| 31 | use common_exception_MissingParameter as MissingParameterException; |
| 32 | use tao_helpers_Http; |
| 33 | |
| 34 | class UploadStylesheetRequestHandler extends ConfigurableService |
| 35 | { |
| 36 | private const VALID_CSS_MIMETYPES = ['text/css']; |
| 37 | |
| 38 | /** |
| 39 | * @throws MissingParameterException |
| 40 | * @throws common_exception_Error |
| 41 | */ |
| 42 | public function __invoke(ServerRequestInterface $request): UploadedStylesheet |
| 43 | { |
| 44 | $params = $request->getQueryParams(); |
| 45 | $this->validate($params); |
| 46 | |
| 47 | $file = $this->getUploadedFileInfo(); |
| 48 | $this->validateUploadedFile($file); |
| 49 | |
| 50 | return new UploadedStylesheet($params['uri'], $file['name'], $file['tmp_name']); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @throws MissingParameterException |
| 55 | */ |
| 56 | private function validate(array $params): void |
| 57 | { |
| 58 | RequestValidator::validateRequiredParameters($params, ['uri']); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @throws common_exception_Error |
| 63 | */ |
| 64 | private function getUploadedFileInfo(): array |
| 65 | { |
| 66 | return tao_helpers_Http::getUploadedFile('content'); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @throws FileUploadException|common_exception_Error |
| 71 | */ |
| 72 | private function validateUploadedFile(array $fileInfo): void |
| 73 | { |
| 74 | if (!in_array($fileInfo['type'], self::VALID_CSS_MIMETYPES)) { |
| 75 | throw new FileUploadException(__('The file you tried to upload is not valid')); |
| 76 | } |
| 77 | |
| 78 | RequestValidator::securityCheckPath($fileInfo['name']); |
| 79 | } |
| 80 | } |