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