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