Skip to content

Commit 11a6c7e

Browse files
committed
Allow object or array from findOneAndUpdate
1 parent 3444245 commit 11a6c7e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/Queue.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
175175
$resetTimestamp = min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX);
176176

177177
$update = ['$set' => ['earliestGet' => new UTCDateTime($resetTimestamp)]];
178-
$options = ['sort' => ['priority' => 1, 'created' => 1]];
178+
$findOneAndUpdateOptions = ['sort' => ['priority' => 1, 'created' => 1]];
179+
$findOneOptions = ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']];
179180

180181
//ints overflow to floats, should be fine
181182
$end = microtime(true) + ($waitDurationInMillis / 1000.0);
@@ -189,14 +190,14 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
189190
} //@codeCoverageIgnoreEnd
190191

191192
while (true) {
192-
$message = $this->collection->findOneAndUpdate($completeQuery, $update, $options);
193-
//checking if _id exist because findAndModify doesnt seem to return null when it can't match the query on
194-
//older mongo extension
195-
if ($message !== null && array_key_exists('_id', $message)) {
193+
$id = $this->getIdFromMessage(
194+
$this->collection->findOneAndUpdate($completeQuery, $update, $findOneAndUpdateOptions)
195+
);
196+
if ($id !== null) {
196197
// findOneAndUpdate does not correctly return result according to typeMap options so just refetch.
197-
$message = $this->collection->findOne(['_id' => $message->_id]);
198+
$message = $this->collection->findOne(['_id' => $id], $findOneOptions);
198199
//id on left of union operator so a possible id in payload doesnt wipe it out the generated one
199-
return ['id' => $message['_id']] + (array)$message['payload'];
200+
return ['id' => $id] + $message['payload'];
200201
}
201202

202203
if (microtime(true) >= $end) {
@@ -211,6 +212,19 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
211212
}
212213
//@codeCoverageIgnoreEnd
213214

215+
private function getIdFromMessage($message)
216+
{
217+
if (is_array($message)) {
218+
return array_key_exists('_id', $message) ? $message['_id'] : null;
219+
}
220+
221+
if (is_object($message)) {
222+
return isset($message->_id) ? $message->_id : null;
223+
}
224+
225+
return null;
226+
}
227+
214228
/**
215229
* Count queue messages.
216230
*

0 commit comments

Comments
 (0)