|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the Stinger Entity Search package. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
8
|
|
|
* (c) Florian Meyer <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace StingerSoft\EntitySearchBundle\Services; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
|
17
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
|
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
19
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
20
|
|
|
use StingerSoft\EntitySearchBundle\Events\DocumentPreSaveEvent; |
|
21
|
|
|
use StingerSoft\EntitySearchBundle\Model\SearchableAlias; |
|
22
|
|
|
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface; |
|
23
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
24
|
|
|
|
|
25
|
|
|
class DoctrineListener implements EventSubscriber { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @var EntityToDocumentMapperInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $entityToDocumentMapper; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @var SearchService |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $searchService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $needsFlush = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $enableIndexing; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var EventDispatcherInterface|null |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $eventDispatcher; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* DoctrineListener constructor. |
|
56
|
|
|
* @param EntityToDocumentMapperInterface $entityToDocumentMapper |
|
57
|
|
|
* @param SearchService $searchService |
|
58
|
|
|
* @param bool $enableIndexing |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService, $enableIndexing = false) { |
|
61
|
|
|
$this->entityToDocumentMapper = $entityToDocumentMapper; |
|
62
|
|
|
$this->searchService = $searchService; |
|
63
|
|
|
$this->enableIndexing = $enableIndexing; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return array|string[] |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getSubscribedEvents(): array { |
|
70
|
|
|
return array( |
|
71
|
|
|
'postPersist', |
|
72
|
|
|
'postUpdate', |
|
73
|
|
|
'preRemove', |
|
74
|
|
|
'postFlush' |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return EventDispatcherInterface|null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getEventDispatcher(): ?EventDispatcherInterface { |
|
82
|
|
|
return $this->eventDispatcher; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param EventDispatcherInterface|null $eventDispatcher |
|
87
|
|
|
* @required |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher = null): void { |
|
90
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
*/ |
|
97
|
|
|
public function enableIndexing(): void { |
|
98
|
|
|
$this->enableIndexing = true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
*/ |
|
104
|
|
|
public function disableIndexing(): void { |
|
105
|
|
|
$this->enableIndexing = false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
public function isIndexingEnabled(): bool { |
|
112
|
|
|
return $this->enableIndexing; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Index the entity after it is persisted for the first time |
|
117
|
|
|
* |
|
118
|
|
|
* @param LifecycleEventArgs $args |
|
119
|
|
|
*/ |
|
120
|
|
|
public function postPersist(LifecycleEventArgs $args): void { |
|
121
|
|
|
if(!$this->enableIndexing) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param PostFlushEventArgs $eventArgs |
|
129
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
130
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function postFlush(PostFlushEventArgs $eventArgs): void { |
|
133
|
|
|
if($this->needsFlush) { |
|
134
|
|
|
$this->needsFlush = false; |
|
135
|
|
|
$eventArgs->getEntityManager()->flush(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Removes the entity from the index when it marked for deletion |
|
141
|
|
|
* |
|
142
|
|
|
* @param LifecycleEventArgs $args |
|
143
|
|
|
*/ |
|
144
|
|
|
public function preRemove(LifecycleEventArgs $args): void { |
|
145
|
|
|
if(!$this->enableIndexing) { |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
$this->removeEntity($args->getObject(), $args->getObjectManager()); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Updates the entity in the index after it is updated |
|
153
|
|
|
* |
|
154
|
|
|
* @param LifecycleEventArgs $args |
|
155
|
|
|
*/ |
|
156
|
|
|
public function postUpdate(LifecycleEventArgs $args): void { |
|
157
|
|
|
if(!$this->enableIndexing) return; |
|
158
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* |
|
163
|
|
|
* @return EntityToDocumentMapperInterface |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function getEntityToDocumentMapper(): EntityToDocumentMapperInterface { |
|
166
|
|
|
return $this->entityToDocumentMapper; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* |
|
171
|
|
|
* @return SearchService |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getSearchService(): SearchService { |
|
174
|
|
|
return $this->searchService; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* |
|
179
|
|
|
* @param object $entity |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function updateEntity(object $entity, ObjectManager $manager): void { |
|
182
|
|
|
if($entity instanceof SearchableAlias) { |
|
183
|
|
|
$entity = $entity->getEntityToIndex(); |
|
184
|
|
|
} |
|
185
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
|
186
|
|
|
if($document !== null) { |
|
187
|
|
|
if($this->getEventDispatcher()) { |
|
188
|
|
|
$event = new DocumentPreSaveEvent($document); |
|
189
|
|
|
$this->getEventDispatcher()->dispatch($event, DocumentPreSaveEvent::NAME); |
|
190
|
|
|
} |
|
191
|
|
|
$this->getSearchService()->saveDocument($document); |
|
192
|
|
|
$this->needsFlush = true; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* |
|
198
|
|
|
* @param object $entity |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function removeEntity(object $entity, ObjectManager $manager): void { |
|
201
|
|
|
if($entity instanceof SearchableAlias) { |
|
202
|
|
|
$entity = $entity->getEntityToIndex(); |
|
203
|
|
|
} |
|
204
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
|
205
|
|
|
if($document !== null) { |
|
206
|
|
|
$this->getSearchService()->removeDocument($document); |
|
207
|
|
|
$this->needsFlush = true; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.