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

ViewsAnnotationListener::accept()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 19

Duplication

Lines 5
Ratio 21.74 %

Importance

Changes 0
Metric Value
dl 5
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\Views;
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 ReflectionProperty;
15
16
class ViewsAnnotationListener implements PropertyAnnotationListenerInterface
17
{
18
    public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void
19
    {
20
        if (!($annotation instanceof Views)) {
21
            return;
22
        }
23
        $structure = StructureOperations::toMutable($descriptor->getMapping());
24
        $descriptor->setMapping($structure);
25
        $mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property);
26
        $mapping = MappingOperations::toMutable($mapping);
27
        $structure->setProperty($property->getName(), $mapping);
28
        $append = $annotation->mode === Views::MODE_APPEND;
29
        if (empty($annotation->views)) {
30
            $view = ViewOperations::toMutable($mapping->getDefaultView());
31
            $view->setChildViews($annotation->value);
32
            $view->setAppendChildViews($append);
33
            $mapping->setDefaultView($view);
34
            return;
35
        }
36 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...
37
            $view = ViewOperations::toMutable($mapping->getView($name) ?? new View());
38
            $view->setChildViews($annotation->value);
39
            $view->setAppendChildViews($append);
40
            $mapping->setView($name, $view);
41
        }
42
    }
43
}
44