Passed
Push — feature/initial-implementation ( 0c2c6c...79751f )
by Fike
01:54
created

Reader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerListeners() 0 7 1
A read() 0 14 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Annotation\Property;
6
7
use AmaTeam\ElasticSearch\Mapping\Annotation\Property\Listener\ForcedViewListener;
8
use AmaTeam\ElasticSearch\Mapping\Annotation\Property\Listener\ParameterListener;
9
use AmaTeam\ElasticSearch\Mapping\Annotation\Property\Listener\TargetClassListener;
10
use AmaTeam\ElasticSearch\Mapping\Annotation\Property\Listener\TypeListener;
11
use AmaTeam\ElasticSearch\Mapping\PropertyMapping;
12
use AmaTeam\ElasticSearch\Utility\Classes;
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use ReflectionProperty;
15
16
class Reader
17
{
18
    /**
19
     * @var AnnotationListenerInterface[]
20
     */
21
    private $listeners = [];
22
    /**
23
     * @var AnnotationReader
24
     */
25
    private $reader;
26
27
    /**
28
     * @param AnnotationReader $reader
29
     */
30
    public function __construct(AnnotationReader $reader = null)
31
    {
32
        $this->reader = $reader ?? new AnnotationReader();
33
        $this->registerListeners();
34
    }
35
36
    public function read(ReflectionProperty $reflection): PropertyMapping
37
    {
38
        $parent = $reflection->getDeclaringClass()->getName();
39
        $descriptor = new PropertyMapping(
40
            Classes::normalizeAbsoluteName($parent),
41
            $reflection->getName()
42
        );
43
        $annotations = $this->reader->getPropertyAnnotations($reflection);
44
        foreach ($annotations as $annotation) {
45
            foreach ($this->listeners as $listener) {
46
                $listener->accept($reflection, $descriptor, $annotation);
47
            }
48
        }
49
        return $descriptor;
50
    }
51
52
    private function registerListeners()
53
    {
54
        $this->listeners = [
55
            new ForcedViewListener(),
56
            new ParameterListener(),
57
            new TypeListener(),
58
            new TargetClassListener(),
59
        ];
60
    }
61
}
62