You are here

public function SmsQueueProcessor::garbageCollection in SMS Framework 8

Same name and namespace in other branches
  1. 2.x src/Provider/SmsQueueProcessor.php \Drupal\sms\Provider\SmsQueueProcessor::garbageCollection()
  2. 2.1.x src/Provider/SmsQueueProcessor.php \Drupal\sms\Provider\SmsQueueProcessor::garbageCollection()

Delete messages which have been processed and are expired.

Overrides SmsQueueProcessorInterface::garbageCollection

File

src/Provider/SmsQueueProcessor.php, line 93

Class

SmsQueueProcessor
The SMS Queue Processor.

Namespace

Drupal\sms\Provider

Code

public function garbageCollection() {
  $directions = [
    Direction::INCOMING,
    Direction::OUTGOING,
  ];
  $ids = [];

  /** @var \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway */
  foreach ($this->smsGatewayStorage
    ->loadMultiple() as $sms_gateway) {
    foreach ($directions as $direction) {
      $lifetime = $sms_gateway
        ->getRetentionDuration($direction);
      if ($lifetime !== -1) {
        $ids += $this->smsMessageStorage
          ->getQuery()
          ->condition('gateway', $sms_gateway
          ->id(), '=')
          ->condition('queued', 0)
          ->condition('direction', $direction)
          ->condition('processed', NULL, 'IS NOT NULL')
          ->condition('processed', REQUEST_TIME - $lifetime, '<=')
          ->execute();
      }
    }
  }
  if ($ids) {
    $this->smsMessageStorage
      ->delete($this->smsMessageStorage
      ->loadMultiple($ids));
  }
}