Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| MethodAttributeConverter | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
| applyToDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyToStorage | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
7 | |||
| isFileStoragePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\PropertyConverters; |
| 3 | |
| 4 | use Apie\Core\FileStorage\StoredFile; |
| 5 | use Apie\StorageMetadata\Attributes\GetMethodAttribute; |
| 6 | use Apie\StorageMetadata\Attributes\GetMethodOrPropertyAttribute; |
| 7 | use Apie\StorageMetadata\Interfaces\PropertyConverterInterface; |
| 8 | use Apie\StorageMetadata\Mediators\DomainToStorageContext; |
| 9 | use Psr\Http\Message\UploadedFileInterface; |
| 10 | use ReflectionMethod; |
| 11 | |
| 12 | class MethodAttributeConverter implements PropertyConverterInterface |
| 13 | { |
| 14 | public function applyToDomain( |
| 15 | DomainToStorageContext $context |
| 16 | ): void { |
| 17 | // no-op |
| 18 | } |
| 19 | |
| 20 | public function applyToStorage( |
| 21 | DomainToStorageContext $context |
| 22 | ): void { |
| 23 | $storageProperty = $context->storageProperty; |
| 24 | |
| 25 | $propertyAttributes = [ |
| 26 | ...$storageProperty->getAttributes(GetMethodAttribute::class), |
| 27 | ...$storageProperty->getAttributes(GetMethodOrPropertyAttribute::class), |
| 28 | ]; |
| 29 | |
| 30 | foreach ($propertyAttributes as $propertyAttribute) { |
| 31 | $instance = $propertyAttribute->newInstance(); |
| 32 | $domainMethod = $instance->getReflectionMethod($context->domainClass, $context->domainObject); |
| 33 | if ($domainMethod) { |
| 34 | $storagePropertyType = $storageProperty->getType(); |
| 35 | $domainPropertyValue = $domainMethod->invoke($context->domainObject); |
| 36 | if ($this->isFileStoragePath($domainMethod) |
| 37 | && $instance instanceof GetMethodOrPropertyAttribute |
| 38 | && $domainPropertyValue === null |
| 39 | && $context->domainObject instanceof UploadedFileInterface) { |
| 40 | $storedFile = $context->fileStorage->createNewUpload( |
| 41 | $context->domainObject, |
| 42 | get_debug_type($context->domainObject) |
| 43 | ); |
| 44 | $domainPropertyValue = $storedFile->getStoragePath(); |
| 45 | $instance->getReflectionProperty($context->domainClass, $context->domainObject) |
| 46 | ->setValue($context->domainObject, $domainPropertyValue); |
| 47 | } |
| 48 | $storagePropertyValue = $context->dynamicCast($domainPropertyValue, $storagePropertyType); |
| 49 | $context->setStoragePropertyValue($storagePropertyValue); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private function isFileStoragePath(ReflectionMethod $method): bool |
| 55 | { |
| 56 | return $method->name === 'getStoragePath' && $method->getDeclaringClass()->name === StoredFile::class; |
| 57 | } |
| 58 | } |