/**
  * @param RedisMailJob|MailJobInterface $mailJob
  *
  * @return int
  */
 public function enqueue(MailJobInterface $mailJob)
 {
     $timestamp = $mailJob->getTimeToSend();
     $payload = $this->createPayload($mailJob);
     return $timestamp !== null && $timestamp > time() ? $this->getConnection()->getInstance()->zadd($this->queueName . ':delayed', $timestamp, $payload) : $this->getConnection()->getInstance()->rpush($this->queueName, $payload);
 }
 /**
  * 'Ack'knowledge the MailJob. Once a MailJob as been processed it could be:.
  *
  * - Updated its status to 'C'ompleted
  * - Updated its status to 'N'ew and set its `timeToSend` attribute to a future date
  *
  * @param MailJobInterface|PdoMailJob $mailJob
  *
  * @return bool
  */
 public function ack(MailJobInterface $mailJob)
 {
     if ($mailJob->isNewRecord()) {
         throw new InvalidCallException('PdoMailJob cannot be a new object to be acknowledged');
     }
     $sqlText = 'UPDATE `%s`
             SET `attempt`=:attempt, `state`=:state, `timeToSend`=:timeToSend, `sentTime`=:sentTime
             WHERE `id`=:id';
     $sql = sprintf($sqlText, $this->tableName);
     $sentTime = $mailJob->isCompleted() ? date('Y-m-d H:i:s', time()) : null;
     $query = $this->getConnection()->getInstance()->prepare($sql);
     $query->bindValue(':id', $mailJob->getId(), PDO::PARAM_INT);
     $query->bindValue(':attempt', $mailJob->getAttempt(), PDO::PARAM_INT);
     $query->bindValue(':state', $mailJob->getState());
     $query->bindValue(':timeToSend', $mailJob->getTimeToSend());
     $query->bindValue(':sentTime', $sentTime);
     return $query->execute();
 }
 /**
  * @param BeanstalkdMailJob|MailJobInterface $mailJob
  */
 public function ack(MailJobInterface $mailJob)
 {
     if ($mailJob->isNewRecord()) {
         throw new InvalidCallException('BeanstalkdMailJob cannot be a new object to be acknowledged');
     }
     $pheanstalk = $this->getConnection()->getInstance()->useTube($this->queueName);
     if ($mailJob->isCompleted()) {
         $pheanstalk->delete($mailJob->getPheanstalkJob());
     } else {
         $timestamp = $mailJob->getTimeToSend();
         $delay = max(0, $timestamp - time());
         // add back to the queue as it wasn't completed maybe due to some transitory error
         // could also be failed.
         $pheanstalk->release($mailJob->getPheanstalkJob(), Pheanstalk::DEFAULT_PRIORITY, $delay);
     }
 }