@@ -38,29 +38,38 @@ public function __construct(Context $context, $modelClass, array $config)
38
38
public function postUpdate (LifecycleEventArgs $ args )
39
39
{
40
40
if ($ args ->getObject () instanceof $ this ->modelClass ) {
41
- $ this ->scheduledForUpdateIndex [] = ['action ' => SyncProcessor::UPDATE_ACTION , 'args ' => $ args ];
41
+ $ this ->scheduledForUpdateIndex [] = [
42
+ 'action ' => SyncProcessor::UPDATE_ACTION ,
43
+ 'id ' => $ this ->extractId ($ args ->getObject ())
44
+ ];
42
45
}
43
46
}
44
47
45
48
public function postPersist (LifecycleEventArgs $ args )
46
49
{
47
50
if ($ args ->getObject () instanceof $ this ->modelClass ) {
48
- $ this ->scheduledForUpdateIndex [] = ['action ' => SyncProcessor::INSERT_ACTION , 'args ' => $ args ];
51
+ $ this ->scheduledForUpdateIndex [] = [
52
+ 'action ' => SyncProcessor::INSERT_ACTION ,
53
+ 'id ' => $ this ->extractId ($ args ->getObject ())
54
+ ];
49
55
}
50
56
}
51
57
52
58
public function preRemove (LifecycleEventArgs $ args )
53
59
{
54
60
if ($ args ->getObject () instanceof $ this ->modelClass ) {
55
- $ this ->scheduledForUpdateIndex [] = ['action ' => SyncProcessor::REMOVE_ACTION , 'args ' => $ args ];
61
+ $ this ->scheduledForUpdateIndex [] = [
62
+ 'action ' => SyncProcessor::REMOVE_ACTION ,
63
+ 'id ' => $ this ->extractId ($ args ->getObject ())
64
+ ];
56
65
}
57
66
}
58
67
59
68
public function postFlush (PostFlushEventArgs $ event )
60
69
{
61
70
if (count ($ this ->scheduledForUpdateIndex )) {
62
71
foreach ($ this ->scheduledForUpdateIndex as $ updateIndex ) {
63
- $ this ->sendUpdateIndexMessage ($ updateIndex ['action ' ], $ updateIndex ['args ' ]);
72
+ $ this ->sendUpdateIndexMessage ($ updateIndex ['action ' ], $ updateIndex ['id ' ]);
64
73
}
65
74
66
75
$ this ->scheduledForUpdateIndex = [];
@@ -79,17 +88,10 @@ public function getSubscribedEvents()
79
88
80
89
/**
81
90
* @param string $action
82
- * @param LifecycleEventArgs $args
91
+ * @param $id
83
92
*/
84
- private function sendUpdateIndexMessage ($ action , LifecycleEventArgs $ args )
93
+ private function sendUpdateIndexMessage ($ action , $ id )
85
94
{
86
- $ object = $ args ->getObject ();
87
-
88
- $ rp = (new \ReflectionClass ($ this ->modelClass ))->getProperty ($ this ->config ['model_id ' ]);
89
- $ rp ->setAccessible (true );
90
- $ id = $ rp ->getValue ($ object );
91
- $ rp ->setAccessible (false );
92
-
93
95
$ queue = $ this ->context ->createQueue (Commands::SYNC_INDEX_WITH_OBJECT_CHANGE );
94
96
95
97
$ message = $ this ->context ->createMessage (JSON ::encode ([
@@ -104,4 +106,18 @@ private function sendUpdateIndexMessage($action, LifecycleEventArgs $args)
104
106
105
107
$ this ->context ->createProducer ()->send ($ queue , $ message );
106
108
}
109
+
110
+ /**
111
+ * @param $object
112
+ * @return mixed
113
+ */
114
+ private function extractId ($ object )
115
+ {
116
+ $ rp = new \ReflectionProperty ($ object , $ this ->config ['model_id ' ]);
117
+ $ rp ->setAccessible (true );
118
+ $ id = $ rp ->getValue ($ object );
119
+ $ rp ->setAccessible (false );
120
+
121
+ return $ id ;
122
+ }
107
123
}
0 commit comments