1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FtwSoft\NotificationChannels\Intercom; |
4
|
|
|
|
5
|
|
|
use Intercom\IntercomClient; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
8
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
9
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException; |
10
|
|
|
use FtwSoft\NotificationChannels\Intercom\Contracts\IntercomNotification; |
11
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\InvalidArgumentException; |
12
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class IntercomNotificationChannel. |
16
|
|
|
*/ |
17
|
|
|
class IntercomChannel |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Intercom\IntercomClient |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* IntercomNotificationChannel constructor. |
26
|
|
|
* |
27
|
|
|
* @param \Intercom\IntercomClient $client |
28
|
|
|
*/ |
29
|
8 |
|
public function __construct(IntercomClient $client) |
30
|
|
|
{ |
31
|
8 |
|
$this->client = $client; |
32
|
8 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Send the given notification via Intercom API. |
36
|
|
|
* |
37
|
|
|
* @param mixed $notifiable |
38
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
39
|
|
|
* |
40
|
|
|
* @throws \FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException When server responses with a bad HTTP |
41
|
|
|
* code |
42
|
|
|
* @throws \FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException When message is not |
43
|
|
|
* filled correctly |
44
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException Other Guzzle uncatched exceptions |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* |
48
|
|
|
* @see https://developers.intercom.com/intercom-api-reference/reference#admin-initiated-conversation |
49
|
|
|
*/ |
50
|
6 |
|
public function send($notifiable, Notification $notification): void |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
6 |
|
if (! $notification instanceof IntercomNotification) { |
54
|
1 |
|
throw new InvalidArgumentException( |
55
|
1 |
|
sprintf('The notification must implement %s interface', IntercomNotification::class) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
$message = $notification->toIntercom($notifiable); |
60
|
|
|
|
61
|
5 |
|
if (! $message->toIsGiven()) { |
62
|
2 |
|
if (! $to = $notifiable->routeNotificationFor('intercom')) { |
63
|
1 |
|
throw new MessageIsNotCompleteException($message, 'Recipient is not provided'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$message->to($to); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
if (! $message->isValid()) { |
70
|
1 |
|
throw new MessageIsNotCompleteException( |
71
|
1 |
|
$message, |
72
|
1 |
|
'The message is not valid. Please check that you have filled required params' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$this->client->messages->create( |
77
|
3 |
|
$message->toArray() |
78
|
|
|
); |
79
|
4 |
|
} catch (BadResponseException $exception) { |
80
|
1 |
|
event(new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
81
|
|
|
'message' => $message, |
|
|
|
|
82
|
|
|
'error' => $exception->getMessage() |
83
|
|
|
]) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
throw new RequestException($exception, $exception->getMessage(), $exception->getCode()); |
87
|
|
|
} |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \Intercom\IntercomClient |
92
|
|
|
*/ |
93
|
1 |
|
public function getClient(): IntercomClient |
94
|
|
|
{ |
95
|
1 |
|
return $this->client; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|