1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Enity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Services; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Doctrine\Common\EventSubscriber; |
17
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface; |
18
|
|
|
|
19
|
|
|
class DoctrineListener implements EventSubscriber { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @var EntityToDocumentMapperInterface |
24
|
|
|
*/ |
25
|
|
|
protected $entityToDocumentMapper; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* @var SearchService |
30
|
|
|
*/ |
31
|
|
|
protected $searchService; |
32
|
|
|
|
33
|
|
|
public function getSubscribedEvents() { |
34
|
|
|
return array( |
35
|
|
|
'postPersist', |
36
|
|
|
'postUpdate', |
37
|
|
|
'preRemove' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param SearchService $searchService |
45
|
|
|
*/ |
46
|
|
|
public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService) { |
47
|
|
|
$this->entityToDocumentMapper = $entityToDocumentMapper; |
48
|
|
|
$this->searchService = $searchService; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Index the entity after it is persisted for the first time |
53
|
|
|
* |
54
|
|
|
* @param LifecycleEventArgs $args |
55
|
|
|
*/ |
56
|
|
|
public function postPersist(LifecycleEventArgs $args) { |
57
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Removes the entity from the index when it marked for deletion |
62
|
|
|
* |
63
|
|
|
* @param LifecycleEventArgs $args |
64
|
|
|
*/ |
65
|
|
|
public function preRemove(LifecycleEventArgs $args) { |
66
|
|
|
$this->removeEntity($args->getObject(), $args->getObjectManager()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Updates the entity in the index after it is updated |
71
|
|
|
* |
72
|
|
|
* @param LifecycleEventArgs $args |
73
|
|
|
*/ |
74
|
|
|
public function postUpdate(LifecycleEventArgs $args) { |
75
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @return EntityToDocumentMapperInterface |
81
|
|
|
*/ |
82
|
|
|
protected function getEntityToDocumentMapper() { |
83
|
|
|
return $this->entityToDocumentMapper; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
* @return SearchService |
89
|
|
|
*/ |
90
|
|
|
protected function getSearchService(ObjectManager $manager) { |
91
|
|
|
$this->searchService->setObjectManager($manager); |
92
|
|
|
return $this->searchService; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* |
97
|
|
|
* @param object $entity |
98
|
|
|
*/ |
99
|
|
|
protected function updateEntity($entity, ObjectManager $manager) { |
100
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
101
|
|
|
if($document !== false) { |
102
|
|
|
$this->getSearchService($manager)->saveDocument($document); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* @param object $entity |
109
|
|
|
*/ |
110
|
|
|
protected function removeEntity($entity, ObjectManager $manager) { |
111
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
112
|
|
|
if($document !== false) { |
113
|
|
|
$this->getSearchService($manager)->removeDocument($document); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.