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