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

ParameterPropertyAnnotationListener::accept()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 17

Duplication

Lines 10
Ratio 47.62 %

Importance

Changes 0
Metric Value
dl 10
loc 21
c 0
b 0
f 0
rs 8.7624
cc 5
eloc 17
nc 7
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\Parameter\Infrastructure\ParameterAnnotationInterface;
8
use AmaTeam\ElasticSearch\API\Annotation\Mapping\Infrastructure\ViewAwareAnnotationInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Descriptor;
10
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\View;
11
use AmaTeam\ElasticSearch\Entity\Annotation\PropertyAnnotationListenerInterface;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations;
14
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations as StructureOperations;
15
use ReflectionProperty;
16
17
class ParameterPropertyAnnotationListener implements PropertyAnnotationListenerInterface
18
{
19
    public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void
20
    {
21
        if (!($annotation instanceof ParameterAnnotationInterface)) {
22
            return;
23
        }
24
        $structure = StructureOperations::toMutable($descriptor->getMapping());
25
        $descriptor->setMapping($structure);
26
        $mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property);
27
        $mapping = MappingOperations::toMutable($mapping);
28
        $structure->setProperty($property->getName(), $mapping);
29
        $views = $annotation instanceof ViewAwareAnnotationInterface ? $annotation->getViews() : null;
30 View Code Duplication
        if (empty($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...
31
            $view = ViewOperations::toMutable($mapping->getDefaultView());
32
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
33
            $mapping->setDefaultView($view);
34
            return;
35
        }
36 View Code Duplication
        foreach ($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...
37
            $view = ViewOperations::toMutable($mapping->getView($name) ?? new View());
38
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
39
            $mapping->setView($name, $view);
40
        }
41
    }
42
}
43