Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| AddAutoIdGenerator | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| run | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadataBuilder\CodeGenerators; |
| 3 | |
| 4 | use Apie\Core\Identifiers\AutoIncrementInteger; |
| 5 | use Apie\Core\Identifiers\KebabCaseSlug; |
| 6 | use Apie\Core\Utils\ConverterUtils; |
| 7 | use Apie\StorageMetadata\Attributes\OneToOneAttribute; |
| 8 | use Apie\StorageMetadata\Interfaces\AutoIncrementTableInterface; |
| 9 | use Apie\StorageMetadataBuilder\Interfaces\RunGeneratedCodeContextInterface; |
| 10 | use Apie\StorageMetadataBuilder\Mediators\GeneratedCodeContext; |
| 11 | use Nette\PhpGenerator\ClassType; |
| 12 | |
| 13 | /** |
| 14 | * Maps auto increment value objects. This is done with an additional table that just contains id's. |
| 15 | */ |
| 16 | class AddAutoIdGenerator implements RunGeneratedCodeContextInterface |
| 17 | { |
| 18 | public function run(GeneratedCodeContext $generatedCodeContext): void |
| 19 | { |
| 20 | $property = $generatedCodeContext->getCurrentProperty(); |
| 21 | $currentTable = $generatedCodeContext->getCurrentTable(); |
| 22 | if ($property === null || $currentTable === null) { |
| 23 | return; |
| 24 | } |
| 25 | $class = ConverterUtils::toReflectionClass($property); |
| 26 | if ($class === null) { |
| 27 | return; |
| 28 | } |
| 29 | if ($class->isSubclassOf(AutoIncrementInteger::class) || $class->name === AutoIncrementInteger::class) { |
| 30 | $propertyName = 'apie_' |
| 31 | . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property->getDeclaringClass())) |
| 32 | . '_' |
| 33 | . str_replace('-', '_', (string) KebabCaseSlug::fromClass($property)); |
| 34 | $type = $generatedCodeContext->getPrefix('apie_increment_'); |
| 35 | $autoIncrementData = new ClassType($type); |
| 36 | $autoIncrementData->addImplement(AutoIncrementTableInterface::class); |
| 37 | $autoIncrementData->addMethod('getKey') |
| 38 | ->setReturnType('?int') |
| 39 | ->setBody('return $this->id;'); |
| 40 | |
| 41 | $autoIncrementData->addProperty('id')->setType('?int'); |
| 42 | $generatedCodeContext->generatedCode->generatedCodeHashmap[$type] = $autoIncrementData; |
| 43 | $currentTable->addProperty($propertyName) |
| 44 | ->setType($type) |
| 45 | ->addAttribute(OneToOneAttribute::class, [$property->name]); |
| 46 | } |
| 47 | } |
| 48 | } |