2
2
namespace Enqueue \ElasticaBundle \Doctrine ;
3
3
4
4
use Doctrine \Common \Persistence \Event \LifecycleEventArgs ;
5
+ use Doctrine \ORM \Event \PostFlushEventArgs ;
5
6
use Enqueue \ElasticaBundle \Doctrine \Queue \Commands ;
6
7
use Enqueue \ElasticaBundle \Doctrine \Queue \SyncIndexWithObjectChangeProcessor as SyncProcessor ;
7
8
use Enqueue \Util \JSON ;
@@ -17,6 +18,11 @@ final class SyncIndexWithObjectChangeListener implements EventSubscriber
17
18
*/
18
19
private $ modelClass ;
19
20
21
+ /**
22
+ * @var array
23
+ */
24
+ private $ scheduledForUpdateIndex = [];
25
+
20
26
/**
21
27
* @var array
22
28
*/
@@ -31,23 +37,33 @@ public function __construct(Context $context, $modelClass, array $config)
31
37
32
38
public function postUpdate (LifecycleEventArgs $ args )
33
39
{
34
-
35
40
if ($ args ->getObject () instanceof $ this ->modelClass ) {
36
- $ this ->sendUpdateIndexMessage ( SyncProcessor::UPDATE_ACTION , $ args) ;
41
+ $ this ->scheduledForUpdateIndex [] = [ ' action ' => SyncProcessor::UPDATE_ACTION , ' args ' => $ args] ;
37
42
}
38
43
}
39
44
40
45
public function postPersist (LifecycleEventArgs $ args )
41
46
{
42
47
if ($ args ->getObject () instanceof $ this ->modelClass ) {
43
- $ this ->sendUpdateIndexMessage ( SyncProcessor::INSERT_ACTION , $ args) ;
48
+ $ this ->scheduledForUpdateIndex [] = [ ' action ' => SyncProcessor::INSERT_ACTION , ' args ' => $ args] ;
44
49
}
45
50
}
46
51
47
52
public function preRemove (LifecycleEventArgs $ args )
48
53
{
49
54
if ($ args ->getObject () instanceof $ this ->modelClass ) {
50
- $ this ->sendUpdateIndexMessage (SyncProcessor::REMOVE_ACTION , $ args );
55
+ $ this ->scheduledForUpdateIndex [] = ['action ' => SyncProcessor::REMOVE_ACTION , 'args ' => $ args ];
56
+ }
57
+ }
58
+
59
+ public function postFlush (PostFlushEventArgs $ event )
60
+ {
61
+ if (count ($ this ->scheduledForUpdateIndex )) {
62
+ foreach ($ this ->scheduledForUpdateIndex as $ updateIndex ) {
63
+ $ this ->sendUpdateIndexMessage ($ updateIndex ['action ' ], $ updateIndex ['args ' ]);
64
+ }
65
+
66
+ $ this ->scheduledForUpdateIndex = [];
51
67
}
52
68
}
53
69
@@ -57,6 +73,7 @@ public function getSubscribedEvents()
57
73
'postPersist ' ,
58
74
'postUpdate ' ,
59
75
'preRemove ' ,
76
+ 'postFlush '
60
77
];
61
78
}
62
79
0 commit comments