SmsMessage.php in SMS Framework 2.1.x
File
src/Message/SmsMessage.php
View source
<?php
declare (strict_types=1);
namespace Drupal\sms\Message;
use Drupal\sms\Entity\SmsGatewayInterface;
class SmsMessage implements SmsMessageInterface {
protected $uuid;
protected $senderName;
protected $senderPhoneNumber;
protected $recipients = [];
protected $message;
protected $gateway;
protected $direction;
protected $options = [];
protected $result;
protected $uid;
protected $automated = TRUE;
public function __construct($sender_phone_number = NULL, array $recipients = [], $message = '', array $options = [], $uid = NULL) {
$this
->setSenderNumber($sender_phone_number);
$this
->addRecipients($recipients);
$this
->setMessage($message);
$this->message = $message;
$this->options = $options;
$this
->setUid($uid);
$this->uuid = $this
->uuidGenerator()
->generate();
}
public function getSender() {
return $this->senderName;
}
public function setSender($sender) {
$this->senderName = $sender;
return $this;
}
public function getSenderNumber() {
return $this->senderPhoneNumber;
}
public function setSenderNumber($number) {
$this->senderPhoneNumber = $number;
return $this;
}
public function getMessage() {
return $this->message;
}
public function setMessage($message) {
$this->message = $message;
return $this;
}
public function getRecipients() {
return $this->recipients;
}
public function addRecipient($recipient) {
if (!in_array($recipient, $this->recipients)) {
$this->recipients[] = $recipient;
}
return $this;
}
public function addRecipients(array $recipients) {
foreach ($recipients as $recipient) {
$this
->addRecipient($recipient);
}
return $this;
}
public function removeRecipient($recipient) {
$this->recipients = array_values(array_diff($this->recipients, [
$recipient,
]));
return $this;
}
public function removeRecipients(array $recipients) {
$this->recipients = array_values(array_diff($this->recipients, $recipients));
return $this;
}
public function getGateway() {
return $this->gateway;
}
public function setGateway(SmsGatewayInterface $gateway) {
$this->gateway = $gateway;
return $this;
}
public function getDirection() {
return $this->direction;
}
public function setDirection($direction) {
$this->direction = $direction;
return $this;
}
public function getOptions() {
return $this->options;
}
public function getOption($name) {
if (array_key_exists($name, $this->options)) {
return $this->options[$name];
}
return NULL;
}
public function setOption($name, $value) {
$this->options[$name] = $value;
return $this;
}
public function removeOption($name) {
unset($this->options[$name]);
return $this;
}
public function getResult() {
return $this->result;
}
public function setResult(SmsMessageResultInterface $result = NULL) {
$this->result = $result;
return $this;
}
public function getUuid() {
return $this->uuid;
}
public function getUid() {
return $this->uid;
}
public function setUid($uid) {
$this->uid = $uid;
return $this;
}
public function setAutomated($automated) {
$this->automated = $automated;
return $this;
}
public function isAutomated() {
return $this->automated;
}
protected function uuidGenerator() {
return \Drupal::service('uuid');
}
public function chunkByRecipients($size) {
$recipients_all = $this
->getRecipients();
if ($size < 1 || count($recipients_all) <= $size) {
return [
$this,
];
}
$base = clone $this;
$base
->removeRecipients($recipients_all);
$messages = [];
foreach (array_chunk($recipients_all, $size) as $recipients) {
$message = clone $base;
$messages[] = $message
->addRecipients($recipients);
}
return $messages;
}
public function getReport($recipient) {
return $this->result ? $this->result
->getReport($recipient) : NULL;
}
public function getReports() {
return $this->result ? $this->result
->getReports() : [];
}
}