@@ -38,29 +38,38 @@ public function __construct(Context $context, $modelClass, array $config)
3838 public function postUpdate (LifecycleEventArgs $ args )
3939 {
4040 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+ ];
4245 }
4346 }
4447
4548 public function postPersist (LifecycleEventArgs $ args )
4649 {
4750 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+ ];
4955 }
5056 }
5157
5258 public function preRemove (LifecycleEventArgs $ args )
5359 {
5460 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+ ];
5665 }
5766 }
5867
5968 public function postFlush (PostFlushEventArgs $ event )
6069 {
6170 if (count ($ this ->scheduledForUpdateIndex )) {
6271 foreach ($ this ->scheduledForUpdateIndex as $ updateIndex ) {
63- $ this ->sendUpdateIndexMessage ($ updateIndex ['action ' ], $ updateIndex ['args ' ]);
72+ $ this ->sendUpdateIndexMessage ($ updateIndex ['action ' ], $ updateIndex ['id ' ]);
6473 }
6574
6675 $ this ->scheduledForUpdateIndex = [];
@@ -79,17 +88,10 @@ public function getSubscribedEvents()
7988
8089 /**
8190 * @param string $action
82- * @param LifecycleEventArgs $args
91+ * @param $id
8392 */
84- private function sendUpdateIndexMessage ($ action , LifecycleEventArgs $ args )
93+ private function sendUpdateIndexMessage ($ action , $ id )
8594 {
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-
9395 $ queue = $ this ->context ->createQueue (Commands::SYNC_INDEX_WITH_OBJECT_CHANGE );
9496
9597 $ message = $ this ->context ->createMessage (JSON ::encode ([
@@ -104,4 +106,18 @@ private function sendUpdateIndexMessage($action, LifecycleEventArgs $args)
104106
105107 $ this ->context ->createProducer ()->send ($ queue , $ message );
106108 }
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+ }
107123}
0 commit comments