PropertyMap   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 86
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 3 1
A __construct() 0 5 1
A getReflection() 0 3 1
A getAnnotation() 0 3 1
A fromReflectionProperty() 0 27 3
1
<?php
2
3
namespace BitrixToolkit\BitrixEntityMapper\Map;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use InvalidArgumentException;
7
use ReflectionProperty;
8
use BitrixToolkit\BitrixEntityMapper\Annotation\AnnotationReader;
9
use BitrixToolkit\BitrixEntityMapper\Annotation\Property\PropertyAnnotationInterface;
10
11
class PropertyMap
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $code;
17
18
    /**
19
     * @var PropertyAnnotationInterface
20
     */
21
    protected $annotation;
22
23
    /**
24
     * @var ReflectionProperty
25
     */
26
    protected $reflection;
27
28
    /**
29
     * PropertyMap constructor.
30
     * @param string $code
31
     * @param PropertyAnnotationInterface $annotation
32
     * @param ReflectionProperty $reflection
33
     */
34 26
    public function __construct($code, PropertyAnnotationInterface $annotation, ReflectionProperty $reflection)
35
    {
36 26
        $this->code = $code;
37 26
        $this->annotation = $annotation;
38 26
        $this->reflection = $reflection;
39
    }
40
41
    /**
42
     * @param ReflectionProperty $propRef
43
     * @return PropertyMap|null
44
     * @throws AnnotationException
45
     */
46 26
    public static function fromReflectionProperty(ReflectionProperty $propRef)
47
    {
48 26
        $annotationReader = new AnnotationReader();
49 26
        $propAnnotations = $annotationReader->getPropertyAnnotations($propRef);
50 26
        $propAnnotations = array_filter($propAnnotations, function ($propAnnotation) {
51 26
            return $propAnnotation instanceof PropertyAnnotationInterface;
52 26
        });
53
54 26
        if (empty($propAnnotations)) {
55 25
            return null;
56
        }
57
58 26
        if (count($propAnnotations) > 1) {
59 1
            $annotationClasses = array_map(function ($propAnnotation) {
60 1
                return get_class($propAnnotation);
61 1
            }, $propAnnotations);
62
63 1
            throw new InvalidArgumentException(
64 1
                'Аннотации ' . '@' . implode(', @', $annotationClasses) .
65 1
                ' свойства ' . $propRef->getName() . ' класса ' . $propRef->getDeclaringClass()->getName() .
66 1
                ' не могут быть применены одновременно.'
67 1
            );
68
        }
69
70
        /** @var PropertyAnnotationInterface $propAnnotation */
71 25
        $propAnnotation = reset($propAnnotations);
72 25
        return new PropertyMap($propRef->getName(), $propAnnotation, $propRef);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 21
    public function getCode()
79
    {
80 21
        return $this->code;
81
    }
82
83
    /**
84
     * @return PropertyAnnotationInterface
85
     */
86 24
    public function getAnnotation()
87
    {
88 24
        return $this->annotation;
89
    }
90
91
    /**
92
     * @return ReflectionProperty
93
     */
94 6
    public function getReflection()
95
    {
96 6
        return $this->reflection;
97
    }
98
}