Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue #12 #18

Merged
merged 7 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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($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);

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