Completed
Push — master ( 807973...39044b )
by Oleg
04:12
created

ReflectionEntitySpecProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 41
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpec() 0 24 3
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Tests\Api;
5
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\ORM\Mapping\Column;
8
use Doctrine\ORM\Mapping\GeneratedValue;
9
use Doctrine\ORM\Mapping\Id;
10
11
final class ReflectionEntitySpecProvider implements EntitySpecProviderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $entityClassName;
17
18
    public function __construct(string $entityClassName)
19
    {
20
        $this->entityClassName = $entityClassName;
21
    }
22
23
    /**
24
     * @return array
25
     * @throws \Doctrine\Common\Annotations\AnnotationException
26
     * @throws \ReflectionException
27
     */
28
    public function getSpec(): array
29
    {
30
        $spec = [];
31
        $reflectionClassName = new \ReflectionClass($this->entityClassName);
32
        foreach ($reflectionClassName->getProperties() as $property) {
33
            /** @var Column $annotation */
34
            $annotation = (new AnnotationReader())
35
                ->getPropertyAnnotation($property, Column::class);
36
            if (!$annotation) {
37
                continue;
38
            }
39
            $idAnnotation = (new AnnotationReader())
40
                ->getPropertyAnnotation($property, Id::class);
41
            $generated = (new AnnotationReader())
42
                ->getPropertyAnnotation($property, GeneratedValue::class);
43
            $spec[$property->getName()] = [
44
                'required' => !$annotation->nullable,
45
                'type' => $annotation->type,
46
                'is_id' => $idAnnotation !== null,
47
                'is_unique' => $annotation->unique,
48
                'is_generated' => $generated !== null,
49
            ];
50
        }
51
        return $spec;
52
    }
53
}
54