SmsGateway.php in SMS Framework 8
File
src/Entity/SmsGateway.php
View source
<?php
namespace Drupal\sms\Entity;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
use Drupal\sms\Plugin\SmsGatewayPluginCollection;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\sms\Direction;
class SmsGateway extends ConfigEntityBase implements SmsGatewayInterface, EntityWithPluginCollectionInterface {
protected $id;
protected $label;
protected $settings = [];
protected $plugin;
protected $pluginCollection;
protected $skip_queue;
protected $incoming_push_path;
protected $reports_push_path;
protected $retention_duration_incoming;
protected $retention_duration_outgoing;
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
if (!isset($values['incoming_push_path'])) {
$key = Crypt::randomBytesBase64(16);
$values['incoming_push_path'] = '/sms/incoming/receive/' . $key;
}
if (!isset($values['reports_push_path'])) {
$key = Crypt::randomBytesBase64(16);
$values['reports_push_path'] = '/sms/delivery-report/receive/' . $key;
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$original =& $this->original;
$original_path = isset($original) ? $original
->getPushReportPath() : '';
if ($original_path != $this
->getPushReportPath()) {
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
protected function getPluginCollection() {
if (!$this->pluginCollection) {
$this->pluginCollection = new SmsGatewayPluginCollection(\Drupal::service('plugin.manager.sms_gateway'), $this->plugin, $this->settings);
}
return $this->pluginCollection;
}
public function getPluginCollections() {
return [
'settings' => $this
->getPluginCollection(),
];
}
public function getPlugin() {
return $this
->getPluginCollection()
->get($this->plugin);
}
public function getPluginId() {
return $this->plugin;
}
public function getSkipQueue() {
return !empty($this->skip_queue);
}
public function setSkipQueue($skip_queue) {
$this->skip_queue = (bool) $skip_queue;
return $this;
}
public function getPushIncomingPath() {
return $this->incoming_push_path;
}
public function setPushIncomingPath($path) {
$this->incoming_push_path = $path;
return $this;
}
public function getPushReportUrl() {
return Url::fromRoute('sms.delivery_report.receive.' . $this
->id());
}
public function getPushReportPath() {
return $this->reports_push_path;
}
public function setPushReportPath($path) {
$this->reports_push_path = $path;
return $this;
}
public function getRetentionDuration($direction) {
switch ($direction) {
case Direction::INCOMING:
return (int) $this->retention_duration_incoming;
case Direction::OUTGOING:
return (int) $this->retention_duration_outgoing;
default:
throw new \InvalidArgumentException(sprintf('%s is not a valid direction.', $direction));
}
}
public function setRetentionDuration($direction, $retention_duration) {
switch ($direction) {
case Direction::INCOMING:
$this->retention_duration_incoming = $retention_duration;
break;
case Direction::OUTGOING:
$this->retention_duration_outgoing = $retention_duration;
break;
}
return $this;
}
public function getMaxRecipientsOutgoing() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['outgoing_message_max_recipients']) ? (int) $definition['outgoing_message_max_recipients'] : 1;
}
public function supportsIncoming() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['incoming']) ? (bool) $definition['incoming'] : FALSE;
}
public function autoCreateIncomingRoute() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['incoming_route']) ? (bool) $definition['incoming_route'] : FALSE;
}
public function isScheduleAware() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return !empty($definition['schedule_aware']);
}
public function supportsReportsPull() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['reports_pull']) ? (bool) $definition['reports_pull'] : FALSE;
}
public function supportsReportsPush() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['reports_push']) ? (bool) $definition['reports_push'] : FALSE;
}
public function supportsCreditBalanceQuery() {
$definition = $this
->getPlugin()
->getPluginDefinition();
return isset($definition['credit_balance_available']) ? (bool) $definition['credit_balance_available'] : FALSE;
}
}
Classes
Name |
Description |
SmsGateway |
Defines storage for an SMS Gateway instance. |