/**
  * @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;
 }