Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
9 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UploadedFileInterfaceToString | |
81.82% |
9 / 11 |
|
50.00% |
1 / 2 |
7.29 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| convert | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
6.29 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Converters; |
| 3 | |
| 4 | use Apie\Core\Enums\UploadedFileStatus; |
| 5 | use Apie\Core\FileStorage\FileStorageInterface; |
| 6 | use Apie\Core\FileStorage\StoredFile; |
| 7 | use Apie\Core\Utils\ConverterUtils; |
| 8 | use Apie\TypeConverter\ConverterInterface; |
| 9 | use Psr\Http\Message\UploadedFileInterface; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionType; |
| 12 | |
| 13 | /** |
| 14 | * @implements ConverterInterface<?UploadedFileInterface, ?string> |
| 15 | */ |
| 16 | class UploadedFileInterfaceToString implements ConverterInterface |
| 17 | { |
| 18 | public function __construct( |
| 19 | private readonly FileStorageInterface $fileStorage |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public function convert(?UploadedFileInterface $input, ?ReflectionType $wantedType): ?string |
| 24 | { |
| 25 | if ($input === null) { |
| 26 | return null; |
| 27 | } |
| 28 | if ($input instanceof StoredFile && $input->getStoragePath() !== null && $input->getStatus() === UploadedFileStatus::StoredInStorage) { |
| 29 | return $input->getStoragePath(); |
| 30 | } |
| 31 | /** @var ReflectionClass<StoredFile>|null $class */ |
| 32 | $class = ConverterUtils::toReflectionClass($wantedType); |
| 33 | $input = $this->fileStorage->createNewUpload( |
| 34 | $input, |
| 35 | in_array($class?->name, [UploadedFileInterface::class, null]) ? StoredFile::class : $class->name |
| 36 | ); |
| 37 | return $input->getStoragePath(); |
| 38 | } |
| 39 | } |