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\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\Property\View; |
11
|
|
|
use AmaTeam\ElasticSearch\Entity\Annotation\PropertyAnnotationListenerInterface; |
12
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations; |
13
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations; |
14
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\MappingOperations as StructureOperations; |
15
|
|
|
use ReflectionProperty; |
16
|
|
|
|
17
|
|
|
class ParameterPropertyAnnotationListener implements PropertyAnnotationListenerInterface |
18
|
|
|
{ |
19
|
|
|
public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void |
20
|
|
|
{ |
21
|
|
|
if (!($annotation instanceof ParameterAnnotationInterface)) { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
$structure = StructureOperations::toMutable($descriptor->getMapping()); |
25
|
|
|
$descriptor->setMapping($structure); |
26
|
|
|
$mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property); |
27
|
|
|
$mapping = MappingOperations::toMutable($mapping); |
28
|
|
|
$structure->setProperty($property->getName(), $mapping); |
29
|
|
|
$views = $annotation instanceof ViewAwareAnnotationInterface ? $annotation->getViews() : null; |
30
|
|
View Code Duplication |
if (empty($views)) { |
|
|
|
|
31
|
|
|
$view = ViewOperations::toMutable($mapping->getDefaultView()); |
32
|
|
|
$view->setParameter($annotation->getParameter(), $annotation->getValue()); |
33
|
|
|
$mapping->setDefaultView($view); |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
View Code Duplication |
foreach ($views as $name) { |
|
|
|
|
37
|
|
|
$view = ViewOperations::toMutable($mapping->getView($name) ?? new View()); |
38
|
|
|
$view->setParameter($annotation->getParameter(), $annotation->getValue()); |
39
|
|
|
$mapping->setView($name, $view); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
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.