Completed
Push — master ( 39044b...ce5fed )
by Oleg
02:16
created

ReflectionEntitySpecProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 41
ccs 19
cts 20
cp 0.95
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 10
    public function __construct(string $entityClassName)
19
    {
20 10
        $this->entityClassName = $entityClassName;
21 10
    }
22
23
    /**
24
     * @return array
25
     * @throws \Doctrine\Common\Annotations\AnnotationException
26
     * @throws \ReflectionException
27
     */
28 10
    public function getSpec(): array
29
    {
30 10
        $spec = [];
31 10
        $reflectionClassName = new \ReflectionClass($this->entityClassName);
32 10
        foreach ($reflectionClassName->getProperties() as $property) {
33
            /** @var Column $annotation */
34 10
            $annotation = (new AnnotationReader())
35 10
                ->getPropertyAnnotation($property, Column::class);
36 10
            if (!$annotation) {
37
                continue;
38
            }
39 10
            $idAnnotation = (new AnnotationReader())
40 10
                ->getPropertyAnnotation($property, Id::class);
41 10
            $generated = (new AnnotationReader())
42 10
                ->getPropertyAnnotation($property, GeneratedValue::class);
43 10
            $spec[$property->getName()] = [
44 10
                'required' => !$annotation->nullable,
45 10
                'type' => $annotation->type,
46
                'is_id' => $idAnnotation !== null,
47 10
                'is_unique' => $annotation->unique,
48
                'is_generated' => $generated !== null,
49
            ];
50
        }
51 10
        return $spec;
52
    }
53
}
54