Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.58% |
31 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OneToOneAttributeConverter | |
81.58% |
31 / 38 |
|
0.00% |
0 / 2 |
21.26 | |
0.00% |
0 / 1 |
| applyToDomain | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
10.01 | |||
| applyToStorage | |
68.42% |
13 / 19 |
|
0.00% |
0 / 1 |
11.55 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\PropertyConverters; |
| 3 | |
| 4 | use Apie\Core\Attributes\Optional; |
| 5 | use Apie\Core\Utils\ConverterUtils; |
| 6 | use Apie\StorageMetadata\Attributes\OneToOneAttribute; |
| 7 | use Apie\StorageMetadata\Interfaces\PropertyConverterInterface; |
| 8 | use Apie\StorageMetadata\Interfaces\StorageDtoInterface; |
| 9 | use Apie\StorageMetadata\Mediators\DomainToStorageContext; |
| 10 | use ReflectionClass; |
| 11 | |
| 12 | class OneToOneAttributeConverter implements PropertyConverterInterface |
| 13 | { |
| 14 | public function applyToDomain( |
| 15 | DomainToStorageContext $context |
| 16 | ): void { |
| 17 | foreach ($context->storageProperty->getAttributes(OneToOneAttribute::class) as $oneToOneAttribute) { |
| 18 | $domainProperty = $oneToOneAttribute->newInstance()->getReflectionProperty($context->domainClass, $context->domainObject); |
| 19 | if ($domainProperty) { |
| 20 | $storagePropertyValue = $context->getStoragePropertyValue(); |
| 21 | $domainPropertyType = $domainProperty->getType(); |
| 22 | if ($domainProperty->isInitialized($context->domainObject) && $storagePropertyValue instanceof StorageDtoInterface) { |
| 23 | $domainPropertyValue = $domainProperty->getValue($context->domainObject); |
| 24 | if ($domainPropertyValue) { |
| 25 | $context->domainToStorageConverter->injectExistingDomainObject( |
| 26 | $domainPropertyValue, |
| 27 | $storagePropertyValue |
| 28 | ); |
| 29 | continue; |
| 30 | } |
| 31 | } |
| 32 | $domainPropertyValue = $storagePropertyValue instanceof StorageDtoInterface |
| 33 | ? $context->domainToStorageConverter->createDomainObject($storagePropertyValue) |
| 34 | : $context->dynamicCast($storagePropertyValue, $domainPropertyType); |
| 35 | if (!$domainPropertyType->allowsNull() && $domainPropertyValue === null && $domainProperty->getAttributes(Optional::class)) { |
| 36 | continue; |
| 37 | } |
| 38 | $domainProperty->setValue($context->domainObject, $domainPropertyValue); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function applyToStorage( |
| 44 | DomainToStorageContext $context |
| 45 | ): void { |
| 46 | foreach ($context->storageProperty->getAttributes(OneToOneAttribute::class) as $oneToOneAttribute) { |
| 47 | $domainProperty = $oneToOneAttribute->newInstance()->getReflectionProperty($context->domainClass, $context->domainObject); |
| 48 | if ($domainProperty) { |
| 49 | $storageProperty = $context->storageProperty; |
| 50 | $domainPropertyValue = $domainProperty->isInitialized($context->domainObject) ? $domainProperty->getValue($context->domainObject) : null; |
| 51 | if ($storageProperty->isInitialized($context->storageObject)) { |
| 52 | $storagePropertyValue = $storageProperty->getValue($context->storageObject); |
| 53 | if ($storagePropertyValue instanceof StorageDtoInterface) { |
| 54 | $context->domainToStorageConverter->injectExistingStorageObject( |
| 55 | $domainPropertyValue, |
| 56 | $storagePropertyValue, |
| 57 | $context |
| 58 | ); |
| 59 | continue; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** @var ReflectionClass<StorageDtoInterface>|null $storageClass */ |
| 64 | $storageClass = ConverterUtils::toReflectionClass($storageProperty->getType()); |
| 65 | if ($storageClass && $domainPropertyValue !== null && in_array(StorageDtoInterface::class, $storageClass->getInterfaceNames())) { |
| 66 | $storagePropertyValue = $context->domainToStorageConverter->createStorageObject($domainPropertyValue, $storageClass, $context); |
| 67 | } else { |
| 68 | $storagePropertyValue = $context->dynamicCast($domainPropertyValue, $storageProperty->getType()); |
| 69 | } |
| 70 | $storageProperty->setValue($context->storageObject, $storagePropertyValue); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |