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 FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException; |
9
|
|
|
use FtwSoft\NotificationChannels\Intercom\Contracts\IntercomNotification; |
10
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\InvalidArgumentException; |
11
|
|
|
use FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException; |
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 |
|
$this->sendNotification($notifiable, $notification); |
53
|
1 |
|
} catch (BadResponseException $exception) { |
54
|
1 |
|
throw new RequestException($exception, $exception->getMessage(), $exception->getCode()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
/** |
59
|
|
|
* @return \Intercom\IntercomClient |
60
|
5 |
|
*/ |
61
|
2 |
|
public function getClient(): IntercomClient |
62
|
1 |
|
{ |
63
|
|
|
return $this->client; |
64
|
|
|
} |
65
|
1 |
|
|
66
|
|
|
/** |
67
|
|
|
* @param mixed $notifiable |
68
|
4 |
|
* @param Notification $notification |
69
|
1 |
|
* |
70
|
1 |
|
* @throws MessageIsNotCompleteException |
71
|
1 |
|
* @throws \GuzzleHttp\Exception\GuzzleException |
72
|
|
|
*/ |
73
|
|
|
protected function sendNotification($notifiable, Notification $notification): void |
74
|
|
|
{ |
75
|
3 |
|
if (false === $notification instanceof IntercomNotification) { |
76
|
3 |
|
throw new InvalidArgumentException( |
77
|
|
|
sprintf('The notification must implement %s interface', IntercomNotification::class) |
78
|
4 |
|
); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
2 |
|
/** @var IntercomMessage $message */ |
82
|
|
|
$message = $notification->toIntercom($notifiable); |
83
|
|
|
if (false === $message->toIsGiven()) { |
84
|
|
|
if (false === $to = $notifiable->routeNotificationFor('intercom')) { |
85
|
|
|
throw new MessageIsNotCompleteException($message, 'Recipient is not provided'); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
$message->to($to); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (false === $message->isValid()) { |
92
|
|
|
throw new MessageIsNotCompleteException( |
93
|
|
|
$message, |
94
|
|
|
'The message is not valid. Please check that you have filled required params' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->client->messages->create( |
99
|
|
|
$message->toArray() |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|