Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FromStoredFile
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
5.15
0.00% covered (danger)
0.00%
0 / 1
 supports
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 create
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
1<?php
2namespace Apie\StorageMetadata\ClassInstantiators;
3
4use Apie\Core\FileStorage\StoredFile;
5use Apie\StorageMetadata\Interfaces\ClassInstantiatorInterface;
6use Apie\StorageMetadata\Interfaces\StorageDtoInterface;
7use Psr\Http\Message\UploadedFileInterface;
8use ReflectionClass;
9
10final class FromStoredFile implements ClassInstantiatorInterface
11{
12    public function supports(ReflectionClass $class, ?StorageDtoInterface $storageObject = null): bool
13    {
14        if ($class->name === UploadedFileInterface::class) {
15            return true;
16        }
17        if ($storageObject === null) {
18            return false;
19        }
20        return in_array(UploadedFileInterface::class, $class->getInterfaceNames());
21    }
22
23    public function create(ReflectionClass $class, ?StorageDtoInterface $storageObject = null): object
24    {
25        assert($storageObject !== null);
26        $classReference = $storageObject->getClassReference();
27        if ($classReference->name === UploadedFileInterface::class) {
28            $classReference = new ReflectionClass(StoredFile::class);
29        }
30        return $classReference->getMethod('createFromDto')
31            ->invoke(null, $storageObject);
32    }
33}