Skip to content

Commit

Permalink
Add support for additional setting from configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gdpa committed Dec 13, 2020
1 parent 08738b5 commit 34c64fe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# Changelog

# Changelog
All notable changes to `chabok` will be documented in this file

## 4.1.0 - 2020-12-13

- Add support for additional parameters to be added from config

## 4.0.0 - 2020-12-13

- Support for laravel 8

## 3.0.0 - 2020-12-13

- Support for laravel 7

## 2.0.0 - 2020-12-13

- Support for laravel 6

## 1.0.0 - 2018-08-18

- initial release

15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ Add your Chabok REST API Key to your `config/services.php`:
],
...
```

You can add additional configuration for adding to request from config:
```php
// config/services.php
...
'chabok' => [
'app_id' => env('CHABOK_APP_ID'),
'key' => env('CHABOK_API_KEY'),
'additional' => [
'timeout' => 5,
'request_timeout' => 10
]
],
...
```
**Note: If you want to test chabok set `app_id` to `sandbox`.**

## Usage
Expand Down
10 changes: 6 additions & 4 deletions src/ChabokChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ public function send($notifiable, Notification $notification)
}

$key = config('services.chabok.key');
$additionalConfiguration = config('services.chabok.additional') ?? [];

if (is_null($key)) {
throw InvalidConfiguration::configurationNotSet();
}

$chabokParameters = $notification->toChabok($notifiable)->toArray();

$response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, [
'form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid')),
]);
$params = array_merge(
['form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid'))],
$additionalConfiguration,
);
$response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, $params);

if ($response->getStatusCode() !== 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
Expand Down
25 changes: 25 additions & 0 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ public function it_can_send_a_notification()
$channel->send(new TestNotifiable(), new TestNotification());
}

/** @test */
public function it_can_send_a_notification_with_additional_data_from_config()
{
$additional = ['timeout' => 10, 'connect_timeout' => 10];
$this->app['config']->set('services.chabok.app_id', 'sandbox');
$this->app['config']->set('services.chabok.key', 'ChabokKey');
$this->app['config']->set('services.chabok.additional', $additional);

$response = new Response(200);
$client = Mockery::mock(Client::class);
$client->shouldReceive('post')
->once()
->with('https://sandbox.push.adpdigital.com/api/push/toUsers?access_token=ChabokKey', [
'form_params' => [
'user' => 'UserUUID',
'content' => 'ChabokDescription',
'data' => ['id' => 1],
],
$additional,
])
->andReturn($response);
$channel = new ChabokChannel($client);
$channel->send(new TestNotifiable(), new TestNotification());
}

/** @test */
public function it_throws_an_exception_when_it_is_not_configured()
{
Expand Down

0 comments on commit 34c64fe

Please sign in to comment.