1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity 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
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
19
|
|
|
|
20
|
|
|
class DoctrineListener implements EventSubscriber { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var EntityToDocumentMapperInterface |
25
|
|
|
*/ |
26
|
|
|
protected $entityToDocumentMapper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var SearchService |
31
|
|
|
*/ |
32
|
|
|
protected $searchService; |
33
|
|
|
|
34
|
|
|
protected $needsFlush = false; |
35
|
|
|
|
36
|
|
|
protected $enableIndexing; |
37
|
|
|
|
38
|
|
|
public function getSubscribedEvents() { |
39
|
|
|
return array( |
40
|
|
|
'postPersist', |
41
|
|
|
'postUpdate', |
42
|
|
|
'preRemove', |
43
|
|
|
'postFlush' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* |
50
|
|
|
* @param SearchService $searchService |
51
|
|
|
*/ |
52
|
|
|
public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService, $enableIndexing = true) { |
53
|
|
|
$this->entityToDocumentMapper = $entityToDocumentMapper; |
54
|
|
|
$this->searchService = $searchService; |
55
|
|
|
$this->enableIndexing = $enableIndexing; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function enableIndexing() { |
59
|
|
|
$this->enableIndexing = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function disableIndexing() { |
63
|
|
|
$this->enableIndexing = false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isIndexingEnabled() { |
67
|
|
|
return $this->enableIndexing; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Index the entity after it is persisted for the first time |
72
|
|
|
* |
73
|
|
|
* @param LifecycleEventArgs $args |
74
|
|
|
*/ |
75
|
|
|
public function postPersist(LifecycleEventArgs $args) { |
76
|
|
|
if(!$this->enableIndexing) return; |
77
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function postFlush(PostFlushEventArgs $eventArgs) { |
81
|
|
|
if($this->needsFlush) { |
82
|
|
|
$this->needsFlush = false; |
83
|
|
|
$eventArgs->getEntityManager()->flush(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Removes the entity from the index when it marked for deletion |
89
|
|
|
* |
90
|
|
|
* @param LifecycleEventArgs $args |
91
|
|
|
*/ |
92
|
|
|
public function preRemove(LifecycleEventArgs $args) { |
93
|
|
|
if(!$this->enableIndexing) return; |
94
|
|
|
$this->removeEntity($args->getObject(), $args->getObjectManager()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Updates the entity in the index after it is updated |
99
|
|
|
* |
100
|
|
|
* @param LifecycleEventArgs $args |
101
|
|
|
*/ |
102
|
|
|
public function postUpdate(LifecycleEventArgs $args) { |
103
|
|
|
if(!$this->enableIndexing) return; |
104
|
|
|
$this->updateEntity($args->getObject(), $args->getObjectManager()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return EntityToDocumentMapperInterface |
110
|
|
|
*/ |
111
|
|
|
protected function getEntityToDocumentMapper() { |
112
|
|
|
return $this->entityToDocumentMapper; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* @return SearchService |
118
|
|
|
*/ |
119
|
|
|
protected function getSearchService(ObjectManager $manager) { |
120
|
|
|
$this->searchService->setObjectManager($manager); |
121
|
|
|
return $this->searchService; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* |
126
|
|
|
* @param object $entity |
127
|
|
|
*/ |
128
|
|
|
protected function updateEntity($entity, ObjectManager $manager) { |
129
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
130
|
|
|
if($document !== false) { |
131
|
|
|
$this->getSearchService($manager)->saveDocument($document); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @param object $entity |
138
|
|
|
*/ |
139
|
|
|
protected function removeEntity($entity, ObjectManager $manager) { |
140
|
|
|
$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity); |
141
|
|
|
if($document !== false) { |
142
|
|
|
$this->getSearchService($manager)->removeDocument($document); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
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.