From 34c64fe5a42b0f0603948a813eb05ff99e152286 Mon Sep 17 00:00:00 2001 From: gdpa Date: Sun, 13 Dec 2020 19:50:59 +0300 Subject: [PATCH] Add support for additional setting from configuration --- CHANGELOG.md | 19 ++++++++++++++++++- README.md | 15 ++++++++++++++- src/ChabokChannel.php | 10 ++++++---- tests/ChannelTest.php | 25 +++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04f4111..1d966ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 + diff --git a/README.md b/README.md index b3824e9..43ac643 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ChabokChannel.php b/src/ChabokChannel.php index a6cc5c1..1b26a64 100755 --- a/src/ChabokChannel.php +++ b/src/ChabokChannel.php @@ -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); diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index d1677fa..861be31 100755 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -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() {