diff --git a/README.md b/README.md index c013dda..39456c9 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,25 @@ Add your SmscRu login, secret key (hashed password) and default sender name (or ... ``` +> If smsc.ru is not responding, you MUST set custom host WITH trailing slash +``` +// .env +... +SMSCRU_HOST=http://www1.smsc.kz/ +... +``` + +```php +// config/services.php +... +'smscru' => [ + ... + 'host' => env('SMSCRU_HOST'), + ... +], +... +``` + ## Usage You can use the channel in your `via()` method inside the notification: diff --git a/src/SmscRuApi.php b/src/SmscRuApi.php index 9d1197f..faba6b9 100644 --- a/src/SmscRuApi.php +++ b/src/SmscRuApi.php @@ -3,6 +3,7 @@ namespace NotificationChannels\SmscRu; use DomainException; +use Illuminate\Support\Arr; use GuzzleHttp\Client as HttpClient; use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification; @@ -10,12 +11,12 @@ class SmscRuApi { const FORMAT_JSON = 3; - /** @var string */ - protected $apiUrl = 'https://smsc.ru/sys/send.php'; - /** @var HttpClient */ protected $httpClient; + /** @var string */ + protected $url; + /** @var string */ protected $login; @@ -25,11 +26,12 @@ class SmscRuApi /** @var string */ protected $sender; - public function __construct($login, $secret, $sender) + public function __construct(array $config) { - $this->login = $login; - $this->secret = $secret; - $this->sender = $sender; + $this->url = Arr::get($config, 'host', 'https://smsc.ru/').'sys/send.php'; + $this->login = Arr::get($config, 'login'); + $this->secret = Arr::get($config, 'secret'); + $this->sender = Arr::get($config, 'sender'); $this->httpClient = new HttpClient([ 'timeout' => 5, @@ -57,7 +59,7 @@ public function send($params) $params = array_merge($params, $base); try { - $response = $this->httpClient->post($this->apiUrl, ['form_params' => $params]); + $response = $this->httpClient->post($this->url, ['form_params' => $params]); $response = json_decode((string) $response->getBody(), true); diff --git a/src/SmscRuServiceProvider.php b/src/SmscRuServiceProvider.php index e441dd9..3c112c2 100644 --- a/src/SmscRuServiceProvider.php +++ b/src/SmscRuServiceProvider.php @@ -11,7 +11,7 @@ public function register() $this->app->singleton(SmscRuApi::class, function () { $config = config('services.smscru'); - return new SmscRuApi($config['login'], $config['secret'], $config['sender']); + return new SmscRuApi($config); }); } } diff --git a/tests/SmscRuApiTest.php b/tests/SmscRuApiTest.php new file mode 100644 index 0000000..bafe2b1 --- /dev/null +++ b/tests/SmscRuApiTest.php @@ -0,0 +1,51 @@ +smsc = null; + + parent::tearDown(); + } + + /** @test */ + public function it_has_default_url() + { + $this->smsc = new SmscRuApi([]); + $this->assertAttributeEquals('https://smsc.ru/sys/send.php', 'url', $this->smsc); + $this->assertAttributeEquals(null, 'login', $this->smsc); + $this->assertAttributeEquals(null, 'secret', $this->smsc); + $this->assertAttributeEquals(null, 'sender', $this->smsc); + } + + /** @test */ + public function it_has_custom_config() + { + $host = 'https://smsc.kz/'; + $login = 'login'; + $secret = 'secret'; + $sender = 'sender'; + + $this->smsc = new SmscRuApi([ + 'host' => $host, + 'login' => $login, + 'secret' => $secret, + 'sender' => $sender, + ]); + + $this->assertAttributeEquals('https://smsc.kz/sys/send.php', 'url', $this->smsc); + $this->assertAttributeEquals($login, 'login', $this->smsc); + $this->assertAttributeEquals($secret, 'secret', $this->smsc); + $this->assertAttributeEquals($sender, 'sender', $this->smsc); + } +} diff --git a/tests/SmscRuChannelTest.php b/tests/SmscRuChannelTest.php index 2c2626f..4fe55bf 100644 --- a/tests/SmscRuChannelTest.php +++ b/tests/SmscRuChannelTest.php @@ -35,7 +35,11 @@ public function setUp() { parent::setUp(); - $this->smsc = M::mock(SmscRuApi::class, ['test', 'test', 'John_Doe']); + $this->smsc = M::mock(SmscRuApi::class, [ + 'login' => 'test', + 'secret' => 'test', + 'sender' => 'John_Doe', + ]); $this->channel = new SmscRuChannel($this->smsc); $this->message = M::mock(SmscRuMessage::class); }