Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

ParentsAnnotationListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A accept() 0 11 3
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Annotation\Listener;
4
5
use AmaTeam\ElasticSearch\API\Annotation\Parents;
6
use AmaTeam\ElasticSearch\API\Entity\Descriptor;
7
use AmaTeam\ElasticSearch\Entity\Annotation\StructureAnnotationListenerInterface;
8
use phpDocumentor\Reflection\TypeResolver;
9
use phpDocumentor\Reflection\Types\ContextFactory;
10
use ReflectionClass;
11
12
class ParentsAnnotationListener implements StructureAnnotationListenerInterface
13
{
14
    /**
15
     * @var TypeResolver
16
     */
17
    private $resolver;
18
    /**
19
     * @var ContextFactory
20
     */
21
    private $contextFactory;
22
23
    public function __construct()
24
    {
25
        $this->resolver = new TypeResolver();
26
        $this->contextFactory = new ContextFactory();
27
    }
28
29
    public function accept(ReflectionClass $reflection, Descriptor $descriptor, $annotation): void
30
    {
31
        if (!($annotation instanceof Parents)) {
32
            return;
33
        }
34
        $parents = [];
35
        $context = $this->contextFactory->createFromReflector($reflection);
36
        foreach ($annotation->value as $type) {
37
            $parents[] = (string) $this->resolver->resolve($type, $context);
38
        }
39
        $descriptor->setParentNames($parents);
40
    }
41
}
42