1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Messagebird; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use MessageBird\Exceptions\AuthenticateException; |
8
|
|
|
use MessageBird\Exceptions\BalanceException; |
9
|
|
|
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification; |
10
|
|
|
use MessageBird\Client; |
11
|
|
|
|
12
|
|
|
class MessagebirdChannel |
13
|
|
|
{ |
14
|
|
|
/** @var \MessageBird\Client */ |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
public function __construct(Client $client) |
18
|
|
|
{ |
19
|
|
|
$this->client = $client; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Send the given notification. |
24
|
|
|
* |
25
|
|
|
* @param mixed $notifiable |
26
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
27
|
|
|
* |
28
|
|
|
* @throws \NotificationChannels\MessageBird\Exceptions\CouldNotSendNotification |
29
|
|
|
*/ |
30
|
|
|
public function send($notifiable, Notification $notification) |
31
|
|
|
{ |
32
|
|
|
$message = $notification->toMessagebird($notifiable); |
33
|
|
|
|
34
|
|
|
if (is_string($message)) { |
35
|
|
|
$message = MessagebirdMessage::create($message); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (empty($message->originator)) { |
39
|
|
|
$message->setOriginator(config('services.messagebird.originator')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (empty($message->recipients)) { |
43
|
|
|
$message->setRecipients(config('services.messagebird.recipients')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$this->client->messages->create($message); |
48
|
|
|
} catch (AuthenticateException $exception) { |
49
|
|
|
throw CouldNotSendNotification::couldNotAuthenticate(); |
50
|
|
|
} catch (BalanceException $exception) { |
51
|
|
|
throw CouldNotSendNotification::notEnoughCredits(); |
52
|
|
|
} catch (Exception $exception) { |
53
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($exception); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|