SmsProcessor.php in SMS Framework 8
File
src/Plugin/QueueWorker/SmsProcessor.php
View source
<?php
namespace Drupal\sms\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\sms\Provider\SmsProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\sms\Direction;
class SmsProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $smsProvider;
protected $smsMessageStorage;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SmsProviderInterface $sms_provider) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->smsMessageStorage = $entity_type_manager
->getStorage('sms');
$this->smsProvider = $sms_provider;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('sms.provider'));
}
public function processItem($data) {
if (isset($data['id'])) {
$id = $data['id'];
if ($sms_message = $this->smsMessageStorage
->load($id)) {
switch ($sms_message
->getDirection()) {
case Direction::INCOMING:
$this->smsProvider
->incoming($sms_message);
break;
case Direction::OUTGOING:
$this->smsProvider
->send($sms_message);
break;
}
$duration = NULL;
if ($gateway = $sms_message
->getGateway()) {
$duration = $gateway
->getRetentionDuration($sms_message
->getDirection());
}
if ($duration === 0) {
$sms_message
->delete();
}
else {
$sms_message
->setProcessedTime(REQUEST_TIME)
->setQueued(FALSE)
->save();
}
}
}
}
}