SmsDeliveryReport.php in SMS Framework 8
File
src/Entity/SmsDeliveryReport.php
View source
<?php
namespace Drupal\sms\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\sms\Exception\SmsStorageException;
use Drupal\sms\Message\SmsMessageReportStatus;
use Drupal\sms\Message\SmsDeliveryReportInterface as StdDeliveryReportInterface;
class SmsDeliveryReport extends ContentEntityBase implements SmsDeliveryReportInterface {
use EntityChangedTrait;
public function getMessageId() {
return $this
->get('message_id')->value;
}
public function setMessageId($message_id) {
$this
->set('message_id', $message_id);
return $this;
}
public function getRecipient() {
return $this
->get('recipient')->value;
}
public function setRecipient($recipient) {
$this
->set('recipient', $recipient);
return $this;
}
public function getStatus() {
return $this
->get('status')->value;
}
public function setStatus($status) {
$this
->set('status', $status);
return $this;
}
public function getStatusMessage() {
return $this
->get('status_message')->value;
}
public function setStatusMessage($message) {
$this
->set('status_message', $message);
return $this;
}
public function getStatusTime() {
return $this
->get('status_time')->value;
}
public function setStatusTime($time) {
$this
->set('status_time', $time);
return $this;
}
public function getTimeQueued() {
$queued = $this
->getRevisionAtStatus(SmsMessageReportStatus::QUEUED);
return $queued ? $queued
->getStatusTime() : NULL;
}
public function setTimeQueued($time) {
$this
->setStatus(SmsMessageReportStatus::QUEUED)
->setStatusTime($time);
return $this;
}
public function getTimeDelivered() {
$delivered = $this
->getRevisionAtStatus(SmsMessageReportStatus::DELIVERED);
return $delivered ? $delivered
->getStatusTime() : NULL;
}
public function setTimeDelivered($time) {
$this
->setStatus(SmsMessageReportStatus::DELIVERED)
->setStatusTime($time);
return $this;
}
public function getSmsMessage() {
return $this
->get('sms_message')->entity;
}
public function setSmsMessage(SmsMessageInterface $sms_message) {
$this
->set('sms_message', $sms_message);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['message_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Message ID'))
->setDescription(t('The message ID assigned to the message.'))
->setReadOnly(TRUE)
->setDefaultValue('');
$fields['recipient'] = BaseFieldDefinition::create('string')
->setLabel(t('Recipient number'))
->setDescription(t('The phone number of the recipient of the message.'))
->setReadOnly(TRUE)
->setDefaultValue('')
->setRequired(TRUE);
$fields['status'] = BaseFieldDefinition::create('string')
->setLabel(t('Delivery status'))
->setDescription(t('A status code from \\Drupal\\sms\\Message\\SmsMessageReportStatus.'))
->setReadOnly(TRUE)
->setRequired(TRUE)
->setRevisionable(TRUE);
$fields['status_message'] = BaseFieldDefinition::create('string')
->setLabel(t('Status message'))
->setDescription(t('The status message as provided by the gateway API.'))
->setReadOnly(TRUE)
->setDefaultValue('')
->setRequired(FALSE)
->setRevisionable(TRUE);
$fields['status_time'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Status time'))
->setDescription(t('The time for the current delivery report status.'))
->setReadOnly(TRUE)
->setRequired(TRUE)
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time the entity was last updated.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['sms_message'] = BaseFieldDefinition::create('entity_reference')
->setSetting('target_type', 'sms')
->setLabel(t('SMS Message'))
->setDescription(t('The parent SMS message.'))
->setReadOnly(TRUE)
->setRequired(TRUE);
return $fields;
}
public function getRevisionAtStatus($status) {
$storage = $this
->entityTypeManager()
->getStorage($this->entityTypeId);
$revision_ids = $storage
->getQuery()
->allRevisions()
->condition($this
->getEntityType()
->getKey('id'), $this
->id())
->condition('status', $status)
->sort($this
->getEntityType()
->getKey('revision'), 'DESC')
->range(0, 1)
->execute();
if ($revision_ids) {
return $storage
->loadRevision(key($revision_ids));
}
return NULL;
}
public function preSave(EntityStorageInterface $storage) {
if (!$this
->getSmsMessage()) {
throw new SmsStorageException('No parent SMS message specified for SMS delivery report');
}
parent::preSave($storage);
}
public function save() {
$this
->setNewRevision(TRUE);
return parent::save();
}
public static function convertFromDeliveryReport(StdDeliveryReportInterface $sms_report) {
if ($sms_report instanceof SmsDeliveryReportInterface) {
return $sms_report;
}
$new = SmsDeliveryReport::create();
$new
->setMessageId($sms_report
->getMessageId())
->setRecipient($sms_report
->getRecipient())
->setStatus($sms_report
->getStatus())
->setStatusMessage($sms_report
->getStatusMessage())
->setStatusTime($sms_report
->getStatusTime());
return $new;
}
}