SmsQueueProcessor.php in SMS Framework 2.x
File
src/Provider/SmsQueueProcessor.php
View source
<?php
declare (strict_types=1);
namespace Drupal\sms\Provider;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\sms\Direction;
class SmsQueueProcessor implements SmsQueueProcessorInterface {
protected $smsGatewayStorage;
protected $smsMessageStorage;
protected $queue;
protected $smsProvider;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, QueueFactory $queue_factory, SmsProviderInterface $sms_provider, TimeInterface $time) {
$this->smsGatewayStorage = $entity_type_manager
->getStorage('sms_gateway');
$this->smsMessageStorage = $entity_type_manager
->getStorage('sms');
$this->queue = $queue_factory
->get('sms.messages', FALSE);
$this->smsProvider = $sms_provider;
$this->time = $time;
}
public function processUnqueued() {
$ids = [];
foreach ($this->smsGatewayStorage
->loadMultiple() as $sms_gateway) {
$query = $this->smsMessageStorage
->getQuery()
->condition('gateway', $sms_gateway
->id(), '=')
->condition('queued', 0, '=')
->condition('processed', NULL, 'IS NULL');
if (!$sms_gateway
->isScheduleAware()) {
$query
->condition('send_on', $this->time
->getRequestTime(), '<=');
}
$ids += $query
->execute();
}
foreach ($this->smsMessageStorage
->loadMultiple($ids) as $sms_message) {
$data = [
'id' => $sms_message
->id(),
];
if ($this->queue
->createItem($data)) {
$sms_message
->setQueued(TRUE)
->save();
}
}
}
public function garbageCollection() {
$directions = [
Direction::INCOMING,
Direction::OUTGOING,
];
$ids = [];
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', $this->time
->getRequestTime() - $lifetime, '<=')
->execute();
}
}
}
if ($ids) {
$this->smsMessageStorage
->delete($this->smsMessageStorage
->loadMultiple($ids));
}
}
}