EntityMap   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
eloc 30
dl 0
loc 114
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getReflection() 0 3 1
A __construct() 0 6 1
A getClass() 0 3 1
A getProperties() 0 3 1
A getProperty() 0 9 3
A fromClass() 0 22 5
A getAnnotation() 0 3 1
1
<?php
2
3
namespace BitrixToolkit\BitrixEntityMapper\Map;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use InvalidArgumentException;
7
use ReflectionClass;
8
use ReflectionException;
9
use BitrixToolkit\BitrixEntityMapper\Annotation\AnnotationReader;
10
use BitrixToolkit\BitrixEntityMapper\Annotation\Entity\InfoBlock;
11
12
class EntityMap
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $class;
18
19
    /**
20
     * @var InfoBlock
21
     */
22
    protected $annotation;
23
24
    /**
25
     * @var ReflectionClass
26
     */
27
    protected $reflection;
28
29
    /**
30
     * @var PropertyMap[]
31
     */
32
    protected $properties;
33
34
    /**
35
     * EntityMap constructor.
36
     * @param string $class
37
     * @param InfoBlock $annotation
38
     * @param ReflectionClass $reflection
39
     * @param PropertyMap[] $properties
40
     */
41 25
    public function __construct($class, InfoBlock $annotation, ReflectionClass $reflection, array $properties)
42
    {
43 25
        $this->class = $class;
44 25
        $this->annotation = $annotation;
45 25
        $this->reflection = $reflection;
46 25
        $this->properties = $properties;
47
    }
48
49
    /**
50
     * @param string|object $class
51
     * @return EntityMap
52
     * @throws AnnotationException
53
     * @throws ReflectionException
54
     * @throws InvalidArgumentException
55
     */
56 27
    public static function fromClass($class)
57
    {
58 27
        $class = is_object($class) ? get_class($class) : $class;
59 27
        $annotationReader = new AnnotationReader();
60 27
        $classRef = new ReflectionClass($class);
61
62
        /** @var InfoBlock|null $classAnnotation */
63 27
        $classAnnotation = $annotationReader->getClassAnnotation($classRef, InfoBlock::class);
64 27
        if (!$classAnnotation) {
65 1
            throw new InvalidArgumentException('Нет аннотации @' . InfoBlock::class . ' для класса ' . $classRef->getName() . '.');
66
        }
67
68 26
        $propertyMaps = [];
69 26
        foreach ($classRef->getProperties() as $propRef) {
70 26
            $propertyMap = PropertyMap::fromReflectionProperty($propRef);
71 25
            if ($propertyMap) {
72 25
                $propertyMaps[] = $propertyMap;
73
            }
74
        }
75
76 25
        $entityMap = new self($classRef->getName(), $classAnnotation, $classRef, $propertyMaps);
77 25
        return $entityMap;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 21
    public function getClass()
84
    {
85 21
        return $this->class;
86
    }
87
88
    /**
89
     * @return InfoBlock
90
     */
91 25
    public function getAnnotation()
92
    {
93 25
        return $this->annotation;
94
    }
95
96
    /**
97
     * @return ReflectionClass
98
     */
99 1
    public function getReflection()
100
    {
101 1
        return $this->reflection;
102
    }
103
104
    /**
105
     * @return PropertyMap[]
106
     */
107 22
    public function getProperties()
108
    {
109 22
        return $this->properties;
110
    }
111
112
    /**
113
     * @param string $code
114
     * @return PropertyMap
115
     * @throws InvalidArgumentException
116
     */
117 16
    public function getProperty($code)
118
    {
119 16
        foreach ($this->properties as $property) {
120 16
            if ($property->getCode() === $code) {
121 15
                return $property;
122
            }
123
        }
124
125 1
        throw new InvalidArgumentException("Свойство $code не объявлено в сущности.");
126
    }
127
}