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