1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kerox\Messenger\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Kerox\Messenger\Model\Message; |
7
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
8
|
|
|
use Kerox\Messenger\Request\SendRequest; |
9
|
|
|
use Kerox\Messenger\Response\SendResponse; |
10
|
|
|
|
11
|
|
|
class Send extends AbstractApi |
12
|
|
|
{ |
13
|
|
|
const SENDER_ACTION_TYPING_ON = 'typing_on'; |
14
|
|
|
const SENDER_ACTION_TYPING_OFF = 'typing_off'; |
15
|
|
|
const SENDER_ACTION_MARK_SEEN = 'mark_seen'; |
16
|
|
|
|
17
|
|
|
const NOTIFICATION_TYPE_REGULAR = 'REGULAR'; |
18
|
|
|
const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH'; |
19
|
|
|
const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH'; |
20
|
|
|
|
21
|
|
|
const TAG_SHIPPING_UPDATE = 'SHIPPING_UPDATE'; |
22
|
|
|
const TAG_RESERVATION_UPDATE = 'RESERVATION_UPDATE'; |
23
|
|
|
const TAG_ISSUE_RESOLUTION = 'ISSUE_RESOLUTION'; |
24
|
|
|
const TAG_APPOINTMENT_UPDATE = 'APPOINTMENT_UPDATE'; |
25
|
|
|
const TAG_GAME_EVENT = 'GAME_EVENT'; |
26
|
|
|
const TAG_TRANSPORTATION_UPDATE = 'TRANSPORTATION_UPDATE'; |
27
|
|
|
const TAG_FEATURE_FUNCTIONALITY_UPDATE = 'FEATURE_FUNCTIONALITY_UPDATE'; |
28
|
|
|
const TAG_TICKET_UPDATE = 'TICKET_UPDATE'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|\Kerox\Messenger\Api\Send |
32
|
|
|
*/ |
33
|
|
|
private static $_instance; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Send constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $pageToken |
39
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
40
|
|
|
*/ |
41
|
10 |
|
public function __construct(string $pageToken, ClientInterface $client) |
42
|
|
|
{ |
43
|
10 |
|
parent::__construct($pageToken, $client); |
44
|
10 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $pageToken |
48
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
49
|
|
|
* |
50
|
|
|
* @return \Kerox\Messenger\Api\Send |
51
|
|
|
*/ |
52
|
1 |
|
public static function getInstance(string $pageToken, ClientInterface $client): Send |
53
|
|
|
{ |
54
|
1 |
|
if (self::$_instance === null) { |
55
|
1 |
|
self::$_instance = new self($pageToken, $client); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return self::$_instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $recipient |
63
|
|
|
* @param string|\Kerox\Messenger\Model\Message $message |
64
|
|
|
* @param string $notificationType |
65
|
|
|
* @param string|null $tag |
66
|
|
|
* |
67
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
68
|
|
|
*/ |
69
|
6 |
|
public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR, $tag = null): SendResponse |
70
|
|
|
{ |
71
|
6 |
|
$message = $this->isValidMessage($message); |
72
|
5 |
|
$this->isValidNotificationType($notificationType); |
73
|
|
|
|
74
|
4 |
|
if ($tag !== null) { |
75
|
1 |
|
$this->isValidTag($tag); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
$request = new SendRequest($this->pageToken, $message, $recipient, $notificationType, $tag); |
79
|
3 |
|
$response = $this->client->post('me/messages', $request->build()); |
80
|
|
|
|
81
|
3 |
|
return new SendResponse($response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $recipient |
86
|
|
|
* @param string $action |
87
|
|
|
* @param string $notificationType |
88
|
|
|
* |
89
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
90
|
|
|
*/ |
91
|
2 |
|
public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse |
92
|
|
|
{ |
93
|
2 |
|
$this->isValidAction($action); |
94
|
1 |
|
$this->isValidNotificationType($notificationType); |
95
|
|
|
|
96
|
1 |
|
$request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, SendRequest::TYPE_ACTION); |
97
|
1 |
|
$response = $this->client->post('me/messages', $request->build()); |
98
|
|
|
|
99
|
1 |
|
return new SendResponse($response); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \Kerox\Messenger\Model\Message\Attachment $attachment |
104
|
|
|
* |
105
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
106
|
|
|
*/ |
107
|
1 |
|
public function attachment(Attachment $attachment): SendResponse |
108
|
|
|
{ |
109
|
1 |
|
$message = $this->isValidMessage($attachment); |
110
|
|
|
|
111
|
1 |
|
$request = new SendRequest($this->pageToken, $message); |
112
|
1 |
|
$response = $this->client->post('me/message_attachments', $request->build()); |
113
|
|
|
|
114
|
1 |
|
return new SendResponse($response); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $message |
119
|
|
|
* |
120
|
|
|
* @throws \InvalidArgumentException |
121
|
|
|
* |
122
|
|
|
* @return \Kerox\Messenger\Model\Message |
123
|
|
|
*/ |
124
|
7 |
|
private function isValidMessage($message): Message |
125
|
|
|
{ |
126
|
7 |
|
if ($message instanceof Message) { |
127
|
1 |
|
return $message; |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
if (is_string($message) || $message instanceof Attachment) { |
131
|
5 |
|
return new Message($message); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $notificationType |
139
|
|
|
* |
140
|
|
|
* @throws \InvalidArgumentException |
141
|
|
|
*/ |
142
|
6 |
|
private function isValidNotificationType(string $notificationType) |
143
|
|
|
{ |
144
|
6 |
|
$allowedNotificationType = $this->getAllowedNotificationType(); |
145
|
6 |
|
if (!in_array($notificationType, $allowedNotificationType, true)) { |
146
|
1 |
|
throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType)); |
147
|
|
|
} |
148
|
5 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
6 |
|
private function getAllowedNotificationType(): array |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
6 |
|
self::NOTIFICATION_TYPE_REGULAR, |
157
|
6 |
|
self::NOTIFICATION_TYPE_SILENT_PUSH, |
158
|
6 |
|
self::NOTIFICATION_TYPE_NO_PUSH, |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $action |
164
|
|
|
* |
165
|
|
|
* @throws \InvalidArgumentException |
166
|
|
|
*/ |
167
|
2 |
|
private function isValidAction(string $action) |
168
|
|
|
{ |
169
|
2 |
|
$allowedSenderAction = $this->getAllowedSenderAction(); |
170
|
2 |
|
if (!in_array($action, $allowedSenderAction, true)) { |
171
|
1 |
|
throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction)); |
172
|
|
|
} |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
2 |
|
private function getAllowedSenderAction(): array |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
2 |
|
self::SENDER_ACTION_TYPING_ON, |
182
|
2 |
|
self::SENDER_ACTION_TYPING_OFF, |
183
|
2 |
|
self::SENDER_ACTION_MARK_SEEN, |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $tag |
189
|
|
|
* |
190
|
|
|
* @throws \InvalidArgumentException |
191
|
|
|
*/ |
192
|
1 |
|
private function isValidTag(string $tag) |
193
|
|
|
{ |
194
|
1 |
|
$allowedTag = $this->getAllowedTag(); |
195
|
1 |
|
if (!in_array($tag, $allowedTag, true)) { |
196
|
1 |
|
throw new \InvalidArgumentException('$tag must be either ' . implode(', ', $allowedTag)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
1 |
|
private function getAllowedTag(): array |
204
|
|
|
{ |
205
|
|
|
return [ |
206
|
1 |
|
self::TAG_ISSUE_RESOLUTION, |
207
|
1 |
|
self::TAG_RESERVATION_UPDATE, |
208
|
1 |
|
self::TAG_SHIPPING_UPDATE, |
209
|
1 |
|
self::TAG_APPOINTMENT_UPDATE, |
210
|
1 |
|
self::TAG_GAME_EVENT, |
211
|
1 |
|
self::TAG_TRANSPORTATION_UPDATE, |
212
|
1 |
|
self::TAG_FEATURE_FUNCTIONALITY_UPDATE, |
213
|
1 |
|
self::TAG_TICKET_UPDATE, |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|