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

EntityAnnotationListener::accept()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 3
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
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