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