From dc01c064613e494a2cff354e88e23ec6c97d48c8 Mon Sep 17 00:00:00 2001 From: JhaoDa Date: Thu, 26 Sep 2019 23:21:30 +0700 Subject: [PATCH] Extra API parameters via config; return API response from `send()`; cs --- CHANGELOG.md | 5 +++++ README.md | 10 +++++++--- src/SmscRuApi.php | 7 ++++++- src/SmscRuChannel.php | 12 +++++++----- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5896d1e..9420ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `smsc-ru` will be documented in this file +## 2.0.4 - 2019-09-26 + +- Extra API parameters via config +- Return API response from `send()` + ## 2.0.3 - 2019-08-23 - Fix wrong parameters error in CouldNotSendNotification ([#33](https://github.com/laravel-notification-channels/smsc-ru/issues/33)) diff --git a/README.md b/README.md index eb75029..e4f80b7 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/smsc-ru/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/smsc-ru/?branch=master) [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/smsc-ru.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/smsc-ru) -This package makes it easy to send notifications using [smsc.ru](//smsc.ru) (aka СМС–Центр) with Laravel 5.3+. +This package makes it easy to send notifications using [smsc.ru](https://smsc.ru) (aka СМС–Центр) with Laravel 5.3+. ## Contents - [Installation](#installation) - [Setting up the SmscRu service](#setting-up-the-SmscRu-service) - [Usage](#usage) - - [Available Message methods](#available-message-methods) + - [Available Message methods](#available-methods) - [Changelog](#changelog) - [Testing](#testing) - [Security](#security) @@ -52,7 +52,11 @@ Add your SmscRu login, secret key (hashed password) and default sender name (or 'smscru' => [ 'login' => env('SMSCRU_LOGIN'), 'secret' => env('SMSCRU_SECRET'), - 'sender' => 'John_Doe' + 'sender' => 'John_Doe', + 'extra' => [ + // any other API parameters + // 'tinyurl' => 1 + ], ], ... ``` diff --git a/src/SmscRuApi.php b/src/SmscRuApi.php index 94153e2..d0b3c5b 100644 --- a/src/SmscRuApi.php +++ b/src/SmscRuApi.php @@ -25,6 +25,9 @@ class SmscRuApi /** @var string */ protected $sender; + /** @var array */ + protected $extra; + public function __construct(array $config) { $this->login = Arr::get($config, 'login'); @@ -32,6 +35,8 @@ public function __construct(array $config) $this->sender = Arr::get($config, 'sender'); $this->endpoint = Arr::get($config, 'host', 'https://smsc.ru/').'sys/send.php'; + $this->extra = Arr::get($config, 'extra', []); + $this->client = new HttpClient([ 'timeout' => 5, 'connect_timeout' => 5, @@ -48,7 +53,7 @@ public function send($params) 'fmt' => self::FORMAT_JSON, ]; - $params = \array_merge($base, \array_filter($params)); + $params = \array_merge($base, \array_filter($params), $this->extra); try { $response = $this->client->request('POST', $this->endpoint, ['form_params' => $params]); diff --git a/src/SmscRuChannel.php b/src/SmscRuChannel.php index 7134d84..4a180c2 100644 --- a/src/SmscRuChannel.php +++ b/src/SmscRuChannel.php @@ -21,12 +21,14 @@ public function __construct(SmscRuApi $smsc) * @param mixed $notifiable * @param Notification $notification * - * @return void + * @throws CouldNotSendNotification + * + * @return array|null */ public function send($notifiable, Notification $notification) { if (! ($to = $this->getRecipients($notifiable, $notification))) { - return; + return null; } $message = $notification->{'toSmscRu'}($notifiable); @@ -35,7 +37,7 @@ public function send($notifiable, Notification $notification) $message = new SmscRuMessage($message); } - $this->sendMessage($to, $message); + return $this->sendMessage($to, $message); } /** @@ -50,7 +52,7 @@ protected function getRecipients($notifiable, Notification $notification) { $to = $notifiable->routeNotificationFor('smscru', $notification); - if ($to === null || $to === false || $to === '') { + if (empty($to)) { return []; } @@ -73,6 +75,6 @@ protected function sendMessage($recipients, SmscRuMessage $message) $params['time'] = '0'.$message->sendAt->getTimestamp(); } - $this->smsc->send($params); + return $this->smsc->send($params); } }