Skip to content

Commit

Permalink
Resolved issue #12 (#18)
Browse files Browse the repository at this point in the history
* Resolved issue #12
  • Loading branch information
vanodevium authored and jhaoda committed Mar 30, 2018
1 parent b6d1754 commit 8f94c21
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions src/SmscRuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
namespace NotificationChannels\SmscRu;

use DomainException;
use Illuminate\Support\Arr;
use GuzzleHttp\Client as HttpClient;
use NotificationChannels\SmscRu\Exceptions\CouldNotSendNotification;

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;

Expand All @@ -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,
Expand Down Expand Up @@ -57,7 +59,7 @@ public function send($params)
$params = \array_merge($base, \array_filter($params));

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);

Expand Down
2 changes: 1 addition & 1 deletion src/SmscRuServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
51 changes: 51 additions & 0 deletions tests/SmscRuApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace NotificationChannel\SmscRu\Tests;

use NotificationChannels\SmscRu\SmscRuApi;

class SmscRuApiTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SmscRuApi
*/
private $smsc;

public function tearDown()
{
$this->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);
}
}
6 changes: 5 additions & 1 deletion tests/SmscRuChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 8f94c21

Please sign in to comment.