diff --git a/BaseSmsSender.php b/BaseSmsSender.php new file mode 100644 index 0000000..7668170 --- /dev/null +++ b/BaseSmsSender.php @@ -0,0 +1,171 @@ + $numbers, + 'content' => $content, + 'sender' => $sender, + 'prefix' => $prefix, + 'deliveryTime' => $deliveryTime, + ]); + } + + /** + * {@inheritdoc} + */ + public function send($tel, $message, $sender=NULL, $prefix="+39",$delivery_time=NULL) + { + $message = static::createMessage($tel, $message, $sender=NULL, $prefix="+39",$delivery_time=NULL); + if (!$this->beforeSend($message)) { + return false; + } + try { + $isSuccessful = $this->sendMessage($message); + } catch (Exception $exception) { + $isSuccessful = false; + Yii::error($exception, __METHOD__); + } + $this->afterSend($message, $isSuccessful); + + return $isSuccessful; + } + + /** + * Performs the actual sending of the SMS. + * @param $message BaseSmsSender + * @return bool + */ + abstract public function sendMessage($message); + + /** + * This method is invoked right before an sms is sent. + * You may override this method to do last-minute preparation for the message. + * If you override this method, please make sure you call the parent implementation first. + * @param BaseSmsSender $message + * @return bool whether to continue sending an email. + */ + public function beforeSend($message) + { + $event = new SmsSenderEvent(['message' => $message]); + $this->trigger(self::EVENT_BEFORE_SEND, $event); + + return $event->isValid; + } + + /** + * This method is invoked right after an sms was send. + * You may override this method to do some postprocessing or logging based on sms send status. + * If you override this method, please make sure you call the parent implementation first. + * @param SmsSenderInterface $message + */ + public function afterSend($message) + { + $event = new SmsSenderEvent(['message' => $message]); + $this->trigger(self::EVENT_AFTER_SEND, $event); + } + + /** + * Logs an error message. + * An error message is typically logged when an unrecoverable error occurs + * during the execution of an application. + * @param string|array $message the message to be logged. This can be a simple string or a more + * complex data structure, such as array. + * @param string $category the category of the message. + */ + protected function logError($message, $category) + { + $this->log(Logger::LEVEL_ERROR, $message, $category); + } + + /** + * Logs a debug message. + * Trace messages are logged mainly for development purpose to see + * the execution work flow of some code. This method will only log + * a message when the application is in debug mode. + * @param string|array $message the message to be logged. This can be a simple string or a more + * complex data structure, such as array. + * @param string $category the category of the message. + */ + protected function debug($message, $category) + { + // Logging only if `YII_DEBUG` is `true` since it's a Yii2 standard + if(YII_DEBUG) { + $this->log(Logger::LEVEL_TRACE, $message, $category); + } + } + + /** + * If logging is enable it logs a message with the given type and category. + * @param string|array $message the message to be logged. This can be a simple string or a more + * complex data structure that will be handled by a [[Target|log target]]. + * @param int $level the level of the message. This must be one of the following: + * `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, + * `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`. + * @param string $category the category of the message. + */ + protected function log($level, $message, $category) + { + if(!$this->enableLogging) { // Logging is not enable, skipping + return; + } + Yii::getLogger()->log($message, $level, $category); + } +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1c67a86 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +Yii2 SMS Sender Interface +=========================== + +0.1.1 17 Sep, 2021 +------------------------- + +- Adds BaseSmsSender class and SmsSenderEvent. +- The base sender now triggers events before and after the sending of the message. + +0.1.0 26 Aug, 2021 +------------------------- + +- Initial release. \ No newline at end of file diff --git a/SmsSenderEvent.php b/SmsSenderEvent.php new file mode 100644 index 0000000..5a048ed --- /dev/null +++ b/SmsSenderEvent.php @@ -0,0 +1,28 @@ +