Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
SubObjectCodeGenerator
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
10
100.00% covered (success)
100.00%
1 / 1
 run
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2namespace Apie\StorageMetadataBuilder\CodeGenerators;
3
4use Apie\Core\Attributes\StoreOptions;
5use Apie\Core\Context\ApieContext;
6use Apie\Core\Identifiers\KebabCaseSlug;
7use Apie\Core\Metadata\CompositeMetadata;
8use Apie\Core\Metadata\MetadataFactory;
9use Apie\Core\Utils\ConverterUtils;
10use Apie\StorageMetadata\Attributes\OneToOneAttribute;
11use Apie\StorageMetadataBuilder\Factories\ClassTypeFactory;
12use Apie\StorageMetadataBuilder\Interfaces\RunGeneratedCodeContextInterface;
13use Apie\StorageMetadataBuilder\Mediators\GeneratedCodeContext;
14use Psr\Http\Message\UploadedFileInterface;
15
16/**
17 * Creates the one to many relations for lists.
18 * - create a sub table for the list
19 * - the sub table references the entity with 'parent' property
20 * - an 'order' property is made for the index of the hashmap or the order of the list.
21 */
22final class SubObjectCodeGenerator implements RunGeneratedCodeContextInterface
23{
24    public function run(GeneratedCodeContext $generatedCodeContext): void
25    {
26        $property = $generatedCodeContext->getCurrentProperty();
27        $class = $property ? ConverterUtils::toReflectionClass($property) : null;
28        $currentTable = $generatedCodeContext->getCurrentTable();
29        if (null === $class || null === $currentTable || in_array((string) $property->getType(), [UploadedFileInterface::class])) {
30            return;
31        }
32        foreach ($property->getAttributes(StoreOptions::class) as $attribute) {
33            $options = $attribute->newInstance();
34            if ($options->alwaysMixedData) { // this case is already handled by SimplePropertiesCodeGenerator
35                return;
36            }
37        }
38        $metadata = MetadataFactory::getMetadataStrategyForType($property->getType())
39            ->getResultMetadata(new ApieContext());
40        $propertyName = 'apie_'
41            . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property->getDeclaringClass()))
42            . '_'
43            . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property));
44        if ($currentTable->hasProperty($propertyName)) {
45            return;
46        }
47        if ($metadata instanceof CompositeMetadata) {
48            $tableName = $generatedCodeContext->getPrefix(
49                'apie_resource_'
50            );
51            $table = ClassTypeFactory::createStorageTable($tableName, $class);
52            $generatedCodeContext->withCurrentObject($class)->iterateOverTable($table);
53            $currentTable->addProperty($propertyName)
54                ->setType(($property->getType()->allowsNull() ? '?' : '') . $tableName)
55                ->addAttribute(OneToOneAttribute::class, [$property->name, null, $property->getDeclaringClass()->name]);
56        }
57    }
58}