Skip to content

Commit

Permalink
Merge pull request #28 from subit-io/master
Browse files Browse the repository at this point in the history
Dispatching response and setting reportUrl on message
  • Loading branch information
petericebear committed Mar 14, 2020
2 parents 737624c + 8832a23 commit 28f2de3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -15,7 +15,8 @@
"illuminate/notifications": "^5.5 || ^6.0 || ^7.0",
"illuminate/support": "^5.5 || ^6.0 || ^7.0",
"illuminate/queue": "^5.5 || ^6.0 || ^7.0",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": "^6.2",
"ext-json": "*"
},
"require-dev": {
"mockery/mockery": "^1.0",
Expand Down
29 changes: 27 additions & 2 deletions src/MessagebirdChannel.php
Expand Up @@ -2,16 +2,21 @@

namespace NotificationChannels\Messagebird;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notification;
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification;

class MessagebirdChannel
{
/** @var \NotificationChannels\Messagebird\MessagebirdClient */
protected $client;
private $dispatcher;

public function __construct(MessagebirdClient $client)
public function __construct(MessagebirdClient $client, Dispatcher $dispatcher = null)
{
$this->client = $client;
$this->dispatcher = $dispatcher;
}

/**
Expand All @@ -20,12 +25,15 @@ public function __construct(MessagebirdClient $client)
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return object with response body data if succesful response from API | empty array if not
* @throws \NotificationChannels\MessageBird\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toMessagebird($notifiable);

$data = [];

if (is_string($message)) {
$message = MessagebirdMessage::create($message);
}
Expand All @@ -34,6 +42,23 @@ public function send($notifiable, Notification $notification)
$message->setRecipients($to);
}

$this->client->send($message);
try {
$data = $this->client->send($message);

if ($this->dispatcher !== null) {
$this->dispatcher->dispatch('messagebird-sms', [$notifiable, $notification, $data]);
}
} catch (CouldNotSendNotification $e) {
$this->dispatcher->dispatch(
new NotificationFailed(
$notifiable,
$notification,
'messagebird-sms',
$e->getMessage()
)
);
}

return $data;
}
}
5 changes: 4 additions & 1 deletion src/MessagebirdClient.php
Expand Up @@ -25,6 +25,7 @@ public function __construct(Client $client, $access_key)
/**
* Send the Message.
* @param MessagebirdMessage $message
* @return
* @throws CouldNotSendNotification
*/
public function send(MessagebirdMessage $message)
Expand All @@ -40,12 +41,14 @@ public function send(MessagebirdMessage $message)
}

try {
$this->client->request('POST', 'https://rest.messagebird.com/messages', [
$response = $this->client->request('POST', 'https://rest.messagebird.com/messages', [
'body' => $message->toJson(),
'headers' => [
'Authorization' => 'AccessKey '.$this->access_key,
],
]);

return json_decode($response->getBody()->__toString());
} catch (Exception $exception) {
throw CouldNotSendNotification::serviceRespondedWithAnError($exception);
}
Expand Down
8 changes: 8 additions & 0 deletions src/MessagebirdMessage.php
Expand Up @@ -8,6 +8,7 @@ class MessagebirdMessage
public $originator;
public $recipients;
public $reference;
public $reportUrl;

public static function create($body = '')
{
Expand Down Expand Up @@ -60,6 +61,13 @@ public function setDatacoding($datacoding)
return $this;
}

public function setReportUrl($reportUrl)
{
$this->reportUrl = $reportUrl;

return $this;
}

public function toJson()
{
return json_encode($this);
Expand Down

0 comments on commit 28f2de3

Please sign in to comment.