DominionEnterprises\Mongo\Queue::send PHP Метод

send() публичный Метод

Send a message to the queue.
public send ( array $payload, integer $earliestGet, float $priority ) : void
$payload array the data to store in the message. Data is handled same way as \MongoDB\Collection::insertOne()
$earliestGet integer earliest unix timestamp the message can be retreived.
$priority float priority for order out of get(). 0 is higher priority than 1
Результат void
    public function send(array $payload, $earliestGet = 0, $priority = 0.0)
    {
        if (!is_int($earliestGet)) {
            throw new \InvalidArgumentException('$earliestGet was not an int');
        }
        if (!is_float($priority)) {
            throw new \InvalidArgumentException('$priority was not a float');
        }
        if (is_nan($priority)) {
            throw new \InvalidArgumentException('$priority was NaN');
        }
        //Ensure $earliestGet is between 0 and MONGO_INT32_MAX
        $earliestGet = min(max(0, $earliestGet * 1000), self::MONGO_INT32_MAX);
        $message = ['payload' => $payload, 'running' => false, 'resetTimestamp' => new \MongoDB\BSON\UTCDateTime(self::MONGO_INT32_MAX), 'earliestGet' => new \MongoDB\BSON\UTCDateTime($earliestGet), 'priority' => $priority, 'created' => new \MongoDB\BSON\UTCDateTime((int) (microtime(true) * 1000))];
        $this->collection->insertOne($message);
    }

Usage Example

 /**
  * Verify Queue can be constructed with \MongoDB\Collection
  *
  * @test
  * @covers ::__construct
  *
  * @return void
  */
 public function constructWithCollection()
 {
     $mongo = new \MongoDB\Client($this->mongoUrl, [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
     $collection = $mongo->selectDatabase('testing')->selectCollection('custom_collection');
     $collection->drop();
     $queue = new Queue($collection);
     $payload = ['key1' => 0, 'key2' => true];
     $queue->send($payload, 34, 0.8);
     $expected = ['payload' => $payload, 'running' => false, 'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(), 'earliestGet' => 34, 'priority' => 0.8];
     $this->assertSame(1, $collection->count());
     $message = $collection->findOne();
     $this->assertLessThanOrEqual(time(), $message['created']->toDateTime()->getTimestamp());
     $this->assertGreaterThan(time() - 10, $message['created']->toDateTime()->getTimestamp());
     unset($message['_id'], $message['created']);
     $message['resetTimestamp'] = $message['resetTimestamp']->toDateTime()->getTimestamp();
     $message['earliestGet'] = $message['earliestGet']->toDateTime()->getTimestamp();
     $this->assertSame($expected, $message);
 }