/**
  * @throws AMQPServerException|WrongStateException
  * @return AMQPIncomingMessage
  **/
 public function getNextDelivery()
 {
     if (!$this->consumer instanceof AMQPConsumer) {
         throw new WrongStateException();
     }
     try {
         $obj = $this->lookupQueue($this->consumer->getQueueName());
         $messages = $obj->consume(array('min' => 1, 'max' => 1, 'ack' => (bool) $this->consumer->isAutoAcknowledge()));
     } catch (AMQPQueueException $e) {
         throw new AMQPServerException($e->getMessage(), $e->getCode(), $e);
     }
     $this->checkCommandResult(is_array($messages), "Could not consume from queue");
     $message = array_shift($messages);
     $incoming = AMQPIncomingMessage::spawn($message);
     if ($this->consumer->getConsumerTag() === null) {
         $this->consumer->setConsumerTag($incoming->getConsumerTag());
         $this->consumer->handleConsumeOk($incoming->getConsumerTag());
     } else {
         if ($this->consumer->getConsumerTag() != $incoming->getConsumerTag()) {
             throw new WrongStateException('Consumer change tag');
         }
     }
     $this->consumer->handleDelivery($incoming);
     return $incoming;
 }
 /**
  * @return AMQPChannelInterface
  **/
 public function basicConsume($queue, $autoAck, AMQPConsumer $callback)
 {
     Assert::isInstance($callback, 'AMQPPeclQueueConsumer');
     try {
         $this->consumer = $callback->setQueueName($queue)->setAutoAcknowledge($autoAck === true);
         $obj = $this->lookupQueue($queue);
         $this->consumer->handleConsumeOk($this->consumer->getConsumerTag());
         /**
          * blocking function
          */
         $obj->consume(array($callback, 'handlePeclDelivery'), $autoAck ? AMQP_AUTOACK : self::AMQP_NONE);
     } catch (Exception $e) {
         $this->clearConnection();
         throw new AMQPServerException($e->getMessage(), $e->getCode(), $e);
     }
     return $this;
 }