Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

TargetEntityAnnotationListener::accept()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 19

Duplication

Lines 10
Ratio 43.48 %

Importance

Changes 0
Metric Value
dl 10
loc 23
c 0
b 0
f 0
rs 8.7972
cc 4
eloc 19
nc 4
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Annotation\Listener\Mapping\Property;
6
7
use AmaTeam\ElasticSearch\API\Annotation\Mapping\TargetEntity;
8
use AmaTeam\ElasticSearch\API\Entity\Descriptor;
9
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\View;
10
use AmaTeam\ElasticSearch\Entity\Annotation\PropertyAnnotationListenerInterface;
11
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations as StructureOperations;
14
use phpDocumentor\Reflection\TypeResolver;
15
use phpDocumentor\Reflection\Types\ContextFactory;
16
use ReflectionProperty;
17
18
class TargetEntityAnnotationListener implements PropertyAnnotationListenerInterface
19
{
20
    /**
21
     * @var TypeResolver
22
     */
23
    private $resolver;
24
    /**
25
     * @var ContextFactory
26
     */
27
    private $contextFactory;
28
29
    public function __construct()
30
    {
31
        $this->resolver = new TypeResolver();
32
        $this->contextFactory = new ContextFactory();
33
    }
34
35
    public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void
36
    {
37
        if (!($annotation instanceof TargetEntity)) {
38
            return;
39
        }
40
        $context = $this->contextFactory->createFromReflector($property);
41
        $type = $this->resolver->resolve($annotation->value, $context);
42
        $className = (string) $type;
43
        $structure = StructureOperations::toMutable($descriptor->getMapping());
44
        $descriptor->setMapping($structure);
45
        $Mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property);
46
        $Mapping = MappingOperations::toMutable($Mapping);
47
        $structure->setProperty($property->getName(), $Mapping);
48 View Code Duplication
        if (empty($annotation->views)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $view = ViewOperations::toMutable($Mapping->getDefaultView());
50
            $view->setTargetEntity($className);
51
            $Mapping->setDefaultView($view);
52
            return;
53
        }
54 View Code Duplication
        foreach ($annotation->views as $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $view = ViewOperations::toMutable($Mapping->getView($name) ?? new View());
56
            $view->setTargetEntity($className);
57
            $Mapping->setView($name, $view);
58
        }
59
    }
60
}
61