Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImageToPropertiesHelper | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addImagesToProperties | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| addBase64Image | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| isImageMediaManager | |
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\pciSamples\model\LegacyPciHelper; |
| 24 | |
| 25 | use Exception; |
| 26 | use oat\oatbox\filesystem\Directory; |
| 27 | use oat\taoMediaManager\model\fileManagement\FileManagement; |
| 28 | use oat\taoMediaManager\model\MediaSource; |
| 29 | |
| 30 | class ImageToPropertiesHelper |
| 31 | { |
| 32 | /** @var MediaSource */ |
| 33 | private $mediaSource; |
| 34 | |
| 35 | /** @var FileManagement */ |
| 36 | private $fileManagement; |
| 37 | |
| 38 | public function __construct(MediaSource $mediaSource, FileManagement $fileManagement) |
| 39 | { |
| 40 | $this->mediaSource = $mediaSource; |
| 41 | $this->fileManagement = $fileManagement; |
| 42 | } |
| 43 | |
| 44 | public function addImagesToProperties(array $images, array $properties, Directory $itemDirectory): array |
| 45 | { |
| 46 | foreach ($images as $image) { |
| 47 | if ($this->isImageMediaManager($image['fileName'])) { |
| 48 | $fileInfo = $this->mediaSource->getFileInfo($image['fileName']); |
| 49 | |
| 50 | $data = $this->fileManagement->getFileStream( |
| 51 | $fileInfo['link'] |
| 52 | )->getContents(); |
| 53 | } else { |
| 54 | $data = $itemDirectory->getFile($image['fileName'])->read(); |
| 55 | } |
| 56 | |
| 57 | if (!is_string($data)) { |
| 58 | throw new Exception(sprintf('Failed to get data: %s', $image['fileName'])); |
| 59 | } |
| 60 | |
| 61 | $properties = $this->addBase64Image( |
| 62 | $properties, |
| 63 | $image['fileName'], |
| 64 | $data |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return $properties; |
| 69 | } |
| 70 | |
| 71 | private function addBase64Image(array $properties, string $fileName, string $image): array |
| 72 | { |
| 73 | $properties['content-' . $fileName] = sprintf( |
| 74 | "data:image/png;base64,%s", |
| 75 | base64_encode($image) |
| 76 | ); |
| 77 | |
| 78 | return $properties; |
| 79 | } |
| 80 | |
| 81 | private function isImageMediaManager(string $fileName): bool |
| 82 | { |
| 83 | return false !== strpos($fileName, 'taomedia://'); |
| 84 | } |
| 85 | } |