@@ -186,10 +186,8 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
186
186
$ resetTimestamp = min (max (0 , $ resetTimestamp * 1000 ), self ::MONGO_INT32_MAX );
187
187
188
188
$ update = ['$set ' => ['resetTimestamp ' => new \MongoDB \BSON \UTCDateTime ($ resetTimestamp ), 'running ' => true ]];
189
- $ options = [
190
- 'sort ' => ['priority ' => 1 , 'created ' => 1 ],
191
- 'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' , 'array ' => 'array ' ],
192
- ];
189
+ $ findOneAndUpdateOptions = ['sort ' => ['priority ' => 1 , 'created ' => 1 ]];
190
+ $ findOneOptions = ['typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' , 'array ' => 'array ' ]],
193
191
194
192
//ints overflow to floats, should be fine
195
193
$ end = microtime (true ) + ($ waitDurationInMillis / 1000.0 );
@@ -203,14 +201,14 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
203
201
} //@codeCoverageIgnoreEnd
204
202
205
203
while (true ) {
206
- $ message = $ this ->collection -> findOneAndUpdate ( $ completeQuery , $ update , $ options );
207
- //checking if _id exist because findAndModify doesnt seem to return null when it can't match the query on
208
- //older mongo extension
209
- if ($ message !== null && array_key_exists ( ' _id ' , $ message ) ) {
204
+ $ id = $ this ->getIdFromMessage (
205
+ $ this -> collection -> findOneAndUpdate ( $ completeQuery , $ update , $ findOneAndUpdateOptions )
206
+ );
207
+ if ($ id !== null ) {
210
208
// findOneAndUpdate does not correctly return result according to typeMap options so just refetch.
211
- $ message = $ this ->collection ->findOne (['_id ' => $ message [ ' _id ' ]] );
209
+ $ message = $ this ->collection ->findOne (['_id ' => $ id ], $ findOneOptions );
212
210
//id on left of union operator so a possible id in payload doesnt wipe it out the generated one
213
- return ['id ' => $ message [ ' _id ' ]] + ( array ) $ message ['payload ' ];
211
+ return ['id ' => $ id ] + $ message ['payload ' ];
214
212
}
215
213
216
214
if (microtime (true ) >= $ end ) {
@@ -225,6 +223,19 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
225
223
}
226
224
//@codeCoverageIgnoreEnd
227
225
226
+ private function getIdFromMessage ($ message )
227
+ {
228
+ if (is_array ($ message )) {
229
+ return array_key_exists ('_id ' , $ message ) ? $ message ['_id ' ] : null ;
230
+ }
231
+
232
+ if (is_object ($ message )) {
233
+ return isset ($ message ->_id ) ? $ message ->_id : null ;
234
+ }
235
+
236
+ return null ;
237
+ }
238
+
228
239
/**
229
240
* Count queue messages.
230
241
*
0 commit comments