Passed
Push — feature/initial-implementation ( 89e787...88960a )
by Fike
02:13
created

EntityAnnotationListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A accept() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Entity\Annotation\Listener;
6
7
use AmaTeam\ElasticSearch\API\Annotation\Entity as EntityAnnotation;
8
use AmaTeam\ElasticSearch\API\Entity\Indexing\Indexing;
9
use AmaTeam\ElasticSearch\Entity\Annotation\ClassAnnotationListenerInterface;
10
use AmaTeam\ElasticSearch\Entity\Entity;
11
use ReflectionClass;
12
13
class EntityAnnotationListener implements ClassAnnotationListenerInterface
14
{
15
    public function accept(ReflectionClass $class, Entity $entity, $annotation): void
16
    {
17
        if (!($annotation instanceof EntityAnnotation)) {
18
            return;
19
        }
20
        $writeIndices = $annotation->writeIndices ?: [$class->getShortName()];
21
        $readIndices = $annotation->readIndices ?: [$class->getShortName()];
22
        $target = Indexing::from($entity->getIndexing())
23
            ->setWriteIndices($writeIndices)
24
            ->setReadIndices($readIndices)
25
            ->setType($annotation->type);
26
        $entity
27
            ->setIndexing($target)
28
            ->setRootLevelDocument(true);
29
    }
30
}
31