Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.71% covered (danger)
5.71%
2 / 35
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccessControlListAttributeConverter
5.71% covered (danger)
5.71%
2 / 35
33.33% covered (danger)
33.33%
1 / 3
154.65
0.00% covered (danger)
0.00%
0 / 1
 applyToDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toReflClass
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 applyToStorage
3.23% covered (danger)
3.23%
1 / 31
0.00% covered (danger)
0.00%
0 / 1
100.63
1<?php
2namespace Apie\StorageMetadata\PropertyConverters;
3
4use Apie\Core\Permissions\RequiresPermissionsInterface;
5use Apie\Core\ValueObjects\Utils;
6use Apie\StorageMetadata\Attributes\AclLinkAttribute;
7use Apie\StorageMetadata\Interfaces\PropertyConverterInterface;
8use Apie\StorageMetadata\Interfaces\StorageDtoInterface;
9use Apie\StorageMetadata\Mediators\DomainToStorageContext;
10use ReflectionClass;
11use ReflectionProperty;
12
13class AccessControlListAttributeConverter implements PropertyConverterInterface
14{
15    public function applyToDomain(
16        DomainToStorageContext $context
17    ): void {
18    }
19
20    /**
21     * @template T of object
22     * @param class-string<T> $className
23     * @return ReflectionClass<T>
24     */
25    private function toReflClass(string $className): ReflectionClass
26    {
27        if (str_starts_with($className, 'apie_')) {
28            return new ReflectionClass('Generated\\ApieEntities\\' . $className);
29        }
30        return new ReflectionClass($className);
31    }
32
33    public function applyToStorage(
34        DomainToStorageContext $context
35    ): void {
36        foreach ($context->storageProperty->getAttributes(AclLinkAttribute::class) as $oneToManyAttribute) {
37            $domainPropertyValue = $context->domainObject instanceof RequiresPermissionsInterface
38                ? $context->domainObject->getRequiredPermissions()->toStringList()->toArray()
39                : [];
40            $storageProperties = $context->storageProperty->isInitialized($context->storageObject)
41                ? Utils::toArray($context->storageProperty->getValue($context->storageObject))
42                : [];
43            foreach ($domainPropertyValue as $arrayKey => $arrayValue) {
44                $arrayContext = $context->withArrayKey($arrayKey);
45                $storageClassRefl = $this->toReflClass($oneToManyAttribute->newInstance()->storageClass);
46                if (is_object($arrayValue) && in_array(StorageDtoInterface::class, $storageClassRefl->getInterfaceNames())) {
47                    if (isset($storageProperties[$arrayKey]) && $storageProperties[$arrayKey] instanceof StorageDtoInterface) {
48                        $arrayContext->domainToStorageConverter->injectExistingStorageObject(
49                            $arrayValue,
50                            $storageProperties[$arrayKey],
51                            $arrayContext
52                        );
53                    } else {
54                        $storageProperties[$arrayKey] = $arrayContext->domainToStorageConverter->createStorageObject(
55                            $arrayValue,
56                            $storageClassRefl,
57                            $arrayContext
58                        );
59                    }
60                } else {
61                    $storageProperties[$arrayKey] = $storageClassRefl->newInstance(Utils::toString($arrayValue));
62                    $properties = $storageClassRefl->getProperties(ReflectionProperty::IS_PUBLIC);
63                    foreach ($properties as $property) {
64                        ManyToOneAttributeConverter::applyToProperty(
65                            $property,
66                            $storageProperties[$arrayKey],
67                            $context->storageObject
68                        );
69                    }
70                }
71            }
72            $context->storageProperty->setValue($context->storageObject, $context->dynamicCast($storageProperties, $context->storageProperty->getType()));
73        }
74    }
75}