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\TargetEntity; |
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 phpDocumentor\Reflection\TypeResolver; |
15
|
|
|
use phpDocumentor\Reflection\Types\ContextFactory; |
16
|
|
|
use ReflectionProperty; |
17
|
|
|
|
18
|
|
|
class TargetEntityAnnotationListener implements PropertyAnnotationListenerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var TypeResolver |
22
|
|
|
*/ |
23
|
|
|
private $resolver; |
24
|
|
|
/** |
25
|
|
|
* @var ContextFactory |
26
|
|
|
*/ |
27
|
|
|
private $contextFactory; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->resolver = new TypeResolver(); |
32
|
|
|
$this->contextFactory = new ContextFactory(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function accept(ReflectionProperty $property, Descriptor $descriptor, $annotation): void |
36
|
|
|
{ |
37
|
|
|
if (!($annotation instanceof TargetEntity)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
$context = $this->contextFactory->createFromReflector($property); |
41
|
|
|
$type = $this->resolver->resolve($annotation->value, $context); |
42
|
|
|
$className = (string) $type; |
43
|
|
|
$structure = StructureOperations::toMutable($descriptor->getMapping()); |
44
|
|
|
$descriptor->setMapping($structure); |
45
|
|
|
$Mapping = $structure->getProperty($property->getName()) ?? MappingOperations::fromReflection($property); |
46
|
|
|
$Mapping = MappingOperations::toMutable($Mapping); |
47
|
|
|
$structure->setProperty($property->getName(), $Mapping); |
48
|
|
View Code Duplication |
if (empty($annotation->views)) { |
|
|
|
|
49
|
|
|
$view = ViewOperations::toMutable($Mapping->getDefaultView()); |
50
|
|
|
$view->setTargetEntity($className); |
51
|
|
|
$Mapping->setDefaultView($view); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
View Code Duplication |
foreach ($annotation->views as $name) { |
|
|
|
|
55
|
|
|
$view = ViewOperations::toMutable($Mapping->getView($name) ?? new View()); |
56
|
|
|
$view->setTargetEntity($className); |
57
|
|
|
$Mapping->setView($name, $view); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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.