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

StructureParameterAnnotationListener::accept()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 14

Duplication

Lines 10
Ratio 55.56 %

Importance

Changes 0
Metric Value
dl 10
loc 18
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 14
nc 7
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Annotation\Listener\Mapping\Clazz;
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\Structure\View;
11
use AmaTeam\ElasticSearch\Entity\Annotation\StructureAnnotationListenerInterface;
12
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations;
13
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\ViewOperations;
14
use ReflectionClass;
15
16
class StructureParameterAnnotationListener implements StructureAnnotationListenerInterface
17
{
18
    public function accept(ReflectionClass $reflection, Descriptor $descriptor, $annotation): void
19
    {
20
        if (!($annotation instanceof ParameterAnnotationInterface)) {
21
            return;
22
        }
23
        $mapping = MappingOperations::toMutable($descriptor->getMapping());
24
        $descriptor->setMapping($mapping);
25
        $views = $annotation instanceof ViewAwareAnnotationInterface ? $annotation->getViews() : null;
26 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...
27
            $view = ViewOperations::toMutable($mapping->getDefaultView());
28
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
29
            $mapping->setDefaultView($view);
30
            return;
31
        }
32 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...
33
            $view = ViewOperations::toMutable($mapping->getView($name) ?? new View());
34
            $view->setParameter($annotation->getParameter(), $annotation->getValue());
35
            $mapping->setView($name, $view);
36
        }
37
    }
38
}
39