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) { |
|
|
|
|
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
|
|
|
|
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.