From a92c77655feed39851be77fb4146ab6e4e9f20fa Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 4 Nov 2016 20:48:31 +0900 Subject: [PATCH] Improve error handling --- common/framework/drivers/sms/base.php | 3 ++- common/framework/drivers/sms/coolsms.php | 7 ++++--- common/framework/drivers/sms/dummy.php | 3 ++- common/framework/drivers/smsinterface.php | 3 ++- common/framework/sms.php | 21 ++++++++++++++++----- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/common/framework/drivers/sms/base.php b/common/framework/drivers/sms/base.php index 15c1b5610..0349d5e5a 100644 --- a/common/framework/drivers/sms/base.php +++ b/common/framework/drivers/sms/base.php @@ -99,9 +99,10 @@ abstract class Base implements \Rhymix\Framework\Drivers\SMSInterface * This method returns true on success and false on failure. * * @param array $messages + * @param object $original * @return bool */ - public function send(array $messages) + public function send(array $messages, \Rhymix\Framework\SMS $original) { return false; } diff --git a/common/framework/drivers/sms/coolsms.php b/common/framework/drivers/sms/coolsms.php index b7068afc3..2a9af7df1 100644 --- a/common/framework/drivers/sms/coolsms.php +++ b/common/framework/drivers/sms/coolsms.php @@ -43,9 +43,10 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface * This method returns true on success and false on failure. * * @param array $messages + * @param object $original * @return bool */ - public function send(array $messages) + public function send(array $messages, \Rhymix\Framework\SMS $original) { try { @@ -58,7 +59,7 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface $options->type = $message->type; $options->from = $message->from; $options->to = implode(',', $message->to); - $options->text = $message->content ?: 'SMS'; + $options->text = $message->content ?: $message->type; $options->charset = 'utf8'; if ($message->delay && $message->delay > time()) { @@ -81,7 +82,7 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface if (!$result->success_count) { $error_codes = implode(', ', $result->error_list ?: array('Unknown')); - $message->errors[] = 'Error (' . $error_codes . ') while sending message ' . $i . ' of ' . count($messages) . ' to ' . $options->to; + $original->addError('Error (' . $error_codes . ') while sending message ' . ($i + 1) . ' of ' . count($messages) . ' to ' . $options->to); $status = false; } } diff --git a/common/framework/drivers/sms/dummy.php b/common/framework/drivers/sms/dummy.php index 8c85ca794..d777bf892 100644 --- a/common/framework/drivers/sms/dummy.php +++ b/common/framework/drivers/sms/dummy.php @@ -38,9 +38,10 @@ class Dummy extends Base implements \Rhymix\Framework\Drivers\SMSInterface * This method returns true on success and false on failure. * * @param array $messages + * @param object $original * @return bool */ - public function send(array $messages) + public function send(array $messages, \Rhymix\Framework\SMS $original) { return true; } diff --git a/common/framework/drivers/smsinterface.php b/common/framework/drivers/smsinterface.php index 269b87650..6a88fed10 100644 --- a/common/framework/drivers/smsinterface.php +++ b/common/framework/drivers/smsinterface.php @@ -58,7 +58,8 @@ interface SMSInterface * This method returns true on success and false on failure. * * @param array $messages + * @param object $original * @return bool */ - public function send(array $messages); + public function send(array $messages, \Rhymix\Framework\SMS $original); } diff --git a/common/framework/sms.php b/common/framework/sms.php index 730a75d2e..76ae3fc4c 100644 --- a/common/framework/sms.php +++ b/common/framework/sms.php @@ -11,7 +11,7 @@ class SMS * Instance properties. */ public $driver = null; - public $caller = ''; + protected $caller = ''; protected $from = ''; protected $to = array(); protected $subject = ''; @@ -21,7 +21,7 @@ class SMS protected $force_sms = false; protected $allow_split_sms = true; protected $allow_split_lms = true; - public $errors = array(); + protected $errors = array(); protected $sent = false; /** @@ -474,7 +474,7 @@ class SMS $messages = $this->_formatSpec($this->driver->getAPISpec()); if (count($messages)) { - $this->sent = $this->driver->send($messages) ? true : false; + $this->sent = $this->driver->send($messages, $this) ? true : false; } else { @@ -533,6 +533,17 @@ class SMS return $this->errors; } + /** + * Add an error message. + * + * @param string $message + * @return void + */ + public function addError($message) + { + $this->errors[] = $message; + } + /** * Format the current message according to an API spec. * @@ -574,7 +585,6 @@ class SMS $subject = $this->getSubject(); $content = $this->getContent(); $attachments = $attachments = $this->getAttachments(); - $last_content = 'MMS'; // Determine the message type. if (!$this->isForceSMS() && ($spec['lms_supported'] || $spec['mms_supported'])) @@ -654,6 +664,7 @@ class SMS // Generate a message for each part of the content and attachments. $message_count = max(count($content_parts), count($attachments)); + $last_content = $item->type; for ($i = 1; $i <= $message_count; $i++) { // Get the message content. @@ -663,7 +674,7 @@ class SMS } else { - $item->content = $last_content ?: 'MMS'; + $item->content = $last_content ?: $item->type; } // Get the attachment.