Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
40 / 40 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ClassTypeFactory | |
100.00% |
40 / 40 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| createIndexTable | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| createPrimitiveTable | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| createStorageTable | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadataBuilder\Factories; |
| 3 | |
| 4 | use Apie\Core\Entities\EntityInterface; |
| 5 | use Apie\Core\Entities\PolymorphicEntityInterface; |
| 6 | use Apie\Core\Metadata\MetadataFactory; |
| 7 | use Apie\StorageMetadata\Attributes\DiscriminatorMappingAttribute; |
| 8 | use Apie\StorageMetadata\Attributes\GetMethodAttribute; |
| 9 | use Apie\StorageMetadata\Interfaces\StorageClassInstantiatorInterface; |
| 10 | use Apie\StorageMetadata\Interfaces\StorageDtoInterface; |
| 11 | use Apie\StorageMetadataBuilder\Concerns\IsPolymorphicStorage; |
| 12 | use Apie\StorageMetadataBuilder\Interfaces\MixedStorageInterface; |
| 13 | use Nette\PhpGenerator\ClassType; |
| 14 | use ReflectionClass; |
| 15 | use ReflectionNamedType; |
| 16 | |
| 17 | final class ClassTypeFactory |
| 18 | { |
| 19 | /** |
| 20 | * @codeCoverageIgnore |
| 21 | */ |
| 22 | private function __construct() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | public static function createIndexTable(string $tableName) |
| 27 | { |
| 28 | $class = new ClassType($tableName); |
| 29 | $constructor = $class->addMethod('__construct'); |
| 30 | $constructor->addPromotedParameter('text') |
| 31 | ->setType('string'); |
| 32 | $constructor->addPromotedParameter('priority') |
| 33 | ->setType('int'); |
| 34 | $constructor->addPromotedParameter('idf', 0) |
| 35 | ->setType('float'); |
| 36 | $constructor->addPromotedParameter('tf', 0) |
| 37 | ->setType('float'); |
| 38 | return $class; |
| 39 | } |
| 40 | public static function createPrimitiveTable(string $tableName, ReflectionNamedType $primitiveType): ClassType |
| 41 | { |
| 42 | $table = new ClassType($tableName); |
| 43 | $table->addImplement(MixedStorageInterface::class); |
| 44 | $table->addMethod('__construct') |
| 45 | ->addPromotedParameter('value') |
| 46 | ->setPublic() |
| 47 | ->setType($primitiveType->getName()); |
| 48 | $table->addMethod('toOriginalObject') |
| 49 | ->setReturnType($primitiveType->getName()) |
| 50 | ->setBody('return $this->value;'); |
| 51 | return $table; |
| 52 | } |
| 53 | |
| 54 | public static function createStorageTable(string $tableName, ReflectionClass $referencedObject): ClassType |
| 55 | { |
| 56 | $table = new ClassType($tableName); |
| 57 | $table->addComment($referencedObject->name); |
| 58 | if (in_array(PolymorphicEntityInterface::class, $referencedObject->getInterfaceNames())) { |
| 59 | $table->addImplement(StorageClassInstantiatorInterface::class); |
| 60 | $table->addTrait('\\' . IsPolymorphicStorage::class); |
| 61 | $table->addProperty('discriminatorMapping') |
| 62 | ->setType('array') |
| 63 | ->addAttribute(DiscriminatorMappingAttribute::class); |
| 64 | } else { |
| 65 | $table->addImplement('\\' . StorageDtoInterface::class); |
| 66 | } |
| 67 | if (in_array(EntityInterface::class, $referencedObject->getInterfaceNames())) { |
| 68 | $returnType = $referencedObject->getMethod('getId')->getReturnType(); |
| 69 | $table->addProperty('id') |
| 70 | ->addAttribute(GetMethodAttribute::class, ['getId']) |
| 71 | ->setType('?' . MetadataFactory::getScalarForType($returnType)->value); |
| 72 | } |
| 73 | |
| 74 | $table->addMethod('getClassReference') |
| 75 | ->setStatic(true) |
| 76 | ->setReturnType(ReflectionClass::class) |
| 77 | ->setBody('return new \\ReflectionClass(\\' . $referencedObject->name . '::class);'); |
| 78 | |
| 79 | return $table; |
| 80 | } |
| 81 | } |