Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
9.09% covered (danger)
9.09%
1 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadedFileInterfaceToString
9.09% covered (danger)
9.09%
1 / 11
50.00% covered (danger)
50.00%
1 / 2
43.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convert
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2namespace Apie\StorageMetadata\Converters;
3
4use Apie\Core\Enums\UploadedFileStatus;
5use Apie\Core\FileStorage\FileStorageInterface;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\Core\Utils\ConverterUtils;
8use Apie\TypeConverter\ConverterInterface;
9use Psr\Http\Message\UploadedFileInterface;
10use ReflectionClass;
11use ReflectionType;
12
13/**
14 * @implements ConverterInterface<?UploadedFileInterface, ?string>
15 */
16class 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}