Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.16% |
24 / 38 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AccessControlListAttributeConverter | |
63.16% |
24 / 38 |
|
33.33% |
1 / 3 |
26.25 | |
0.00% |
0 / 1 |
| applyToDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toReflClass | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| applyToStorage | |
60.61% |
20 / 33 |
|
0.00% |
0 / 1 |
18.40 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\PropertyConverters; |
| 3 | |
| 4 | use Apie\Core\Permissions\RequiresPermissionsInterface; |
| 5 | use Apie\Core\ValueObjects\Utils; |
| 6 | use Apie\StorageMetadata\Attributes\AclLinkAttribute; |
| 7 | use Apie\StorageMetadata\Interfaces\PropertyConverterInterface; |
| 8 | use Apie\StorageMetadata\Interfaces\StorageDtoInterface; |
| 9 | use Apie\StorageMetadata\Mediators\DomainToStorageContext; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionProperty; |
| 12 | |
| 13 | class 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, mixed $contextStorageObject): ReflectionClass |
| 26 | { |
| 27 | if (str_starts_with($className, 'apie_') && is_object($contextStorageObject)) { |
| 28 | $refl = new ReflectionClass($contextStorageObject); |
| 29 | return new ReflectionClass($refl->getNamespaceName() . '\\' . $className); |
| 30 | } |
| 31 | return new ReflectionClass($className); |
| 32 | } |
| 33 | |
| 34 | public function applyToStorage( |
| 35 | DomainToStorageContext $context |
| 36 | ): void { |
| 37 | foreach ($context->storageProperty->getAttributes(AclLinkAttribute::class) as $oneToManyAttribute) { |
| 38 | $domainPropertyValue = $context->domainObject instanceof RequiresPermissionsInterface |
| 39 | ? $context->domainObject->getRequiredPermissions()->toStringList()->toArray() |
| 40 | : []; |
| 41 | $storageProperties = $context->storageProperty->isInitialized($context->storageObject) |
| 42 | ? Utils::toArray($context->storageProperty->getValue($context->storageObject)) |
| 43 | : []; |
| 44 | // in case there are no required permissions, we need to add a '' record because we need the record with JOIN |
| 45 | // see RequiresPermissionFilter class. |
| 46 | if (empty($domainPropertyValue)) { |
| 47 | $domainPropertyValue = ['']; |
| 48 | } |
| 49 | foreach ($domainPropertyValue as $arrayKey => $arrayValue) { |
| 50 | $arrayContext = $context->withArrayKey($arrayKey); |
| 51 | $storageClassRefl = $this->toReflClass($oneToManyAttribute->newInstance()->storageClass, $context->storageObject); |
| 52 | if (is_object($arrayValue) && in_array(StorageDtoInterface::class, $storageClassRefl->getInterfaceNames())) { |
| 53 | if (isset($storageProperties[$arrayKey]) && $storageProperties[$arrayKey] instanceof StorageDtoInterface) { |
| 54 | $arrayContext->domainToStorageConverter->injectExistingStorageObject( |
| 55 | $arrayValue, |
| 56 | $storageProperties[$arrayKey], |
| 57 | $arrayContext |
| 58 | ); |
| 59 | } else { |
| 60 | $storageProperties[$arrayKey] = $arrayContext->domainToStorageConverter->createStorageObject( |
| 61 | $arrayValue, |
| 62 | $storageClassRefl, |
| 63 | $arrayContext |
| 64 | ); |
| 65 | } |
| 66 | } else { |
| 67 | $storageProperties[$arrayKey] = $storageClassRefl->newInstance(Utils::toString($arrayValue)); |
| 68 | $properties = $storageClassRefl->getProperties(ReflectionProperty::IS_PUBLIC); |
| 69 | foreach ($properties as $property) { |
| 70 | ManyToOneAttributeConverter::applyToProperty( |
| 71 | $property, |
| 72 | $storageProperties[$arrayKey], |
| 73 | $context->storageObject |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | $context->storageProperty->setValue($context->storageObject, $context->dynamicCast($storageProperties, $context->storageProperty->getType())); |
| 79 | } |
| 80 | } |
| 81 | } |