1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Api; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Helper\ValidatorTrait; |
8
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
9
|
|
|
use Kerox\Messenger\Request\SendRequest; |
10
|
|
|
use Kerox\Messenger\Response\SendResponse; |
11
|
|
|
use Kerox\Messenger\SendInterface; |
12
|
|
|
|
13
|
|
|
class Send extends AbstractApi implements SendInterface |
14
|
|
|
{ |
15
|
|
|
use ValidatorTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $recipient |
19
|
|
|
* @param string|\Kerox\Messenger\Model\Message $message |
20
|
|
|
* @param array $options |
21
|
|
|
* |
22
|
|
|
* @throws \Exception |
23
|
|
|
* |
24
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
25
|
|
|
*/ |
26
|
9 |
|
public function message(string $recipient, $message, array $options = []): SendResponse |
27
|
|
|
{ |
28
|
9 |
|
$message = $this->isValidMessage($message); |
29
|
8 |
|
$options = $this->isValidOptions($options, $message); |
30
|
|
|
|
31
|
3 |
|
$request = new SendRequest($this->pageToken, $message, $recipient, $options); |
32
|
3 |
|
$response = $this->client->post('me/messages', $request->build()); |
33
|
|
|
|
34
|
3 |
|
return new SendResponse($response); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $recipient |
39
|
|
|
* @param string $action |
40
|
|
|
* @param array $options |
41
|
|
|
* |
42
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
43
|
|
|
*/ |
44
|
2 |
|
public function action(string $recipient, string $action, array $options = []): SendResponse |
45
|
|
|
{ |
46
|
2 |
|
$this->isValidSenderAction($action); |
47
|
1 |
|
$options = $this->isValidOptions($options, $action); |
48
|
|
|
|
49
|
1 |
|
$request = new SendRequest($this->pageToken, $action, $recipient, $options, SendRequest::REQUEST_TYPE_ACTION); |
50
|
1 |
|
$response = $this->client->post('me/messages', $request->build()); |
51
|
|
|
|
52
|
1 |
|
return new SendResponse($response); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \Kerox\Messenger\Model\Message\Attachment $attachment |
57
|
|
|
* |
58
|
|
|
* @throws \Exception |
59
|
|
|
* |
60
|
|
|
* @return \Kerox\Messenger\Response\SendResponse |
61
|
|
|
*/ |
62
|
1 |
|
public function attachment(Attachment $attachment): SendResponse |
63
|
|
|
{ |
64
|
1 |
|
$message = $this->isValidMessage($attachment); |
65
|
|
|
|
66
|
1 |
|
$request = new SendRequest($this->pageToken, $message); |
67
|
1 |
|
$response = $this->client->post('me/message_attachments', $request->build()); |
68
|
|
|
|
69
|
1 |
|
return new SendResponse($response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $options |
74
|
|
|
* @param $message |
75
|
|
|
* |
76
|
|
|
* @throws \InvalidArgumentException |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
9 |
|
private function isValidOptions(array $options, $message): array |
81
|
|
|
{ |
82
|
9 |
|
$allowedOptionsKeys = $this->getAllowedOptionsKeys(); |
83
|
9 |
|
foreach ($options as $key => $value) { |
84
|
6 |
|
if (!\in_array($key, $allowedOptionsKeys, true)) { |
85
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
86
|
1 |
|
'Only %s are allowed keys for options.', |
87
|
1 |
|
implode(', ', $allowedOptionsKeys) |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
if ($key === self::OPTION_MESSAGING_TYPE) { |
92
|
2 |
|
$this->isValidMessagingType($value); |
93
|
5 |
|
} elseif ($key === self::OPTION_NOTIFICATION_TYPE) { |
94
|
5 |
|
$this->isValidNotificationType($value); |
95
|
3 |
|
} elseif ($key === self::OPTION_TAG) { |
96
|
4 |
|
$this->isValidTag($value, $message); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return $options; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $messagingType |
105
|
|
|
* |
106
|
|
|
* @throws \InvalidArgumentException |
107
|
|
|
*/ |
108
|
2 |
|
protected function isValidMessagingType(string $messagingType): void |
109
|
|
|
{ |
110
|
2 |
|
$allowedMessagingType = $this->getAllowedMessagingType(); |
111
|
2 |
|
if (!\in_array($messagingType, $allowedMessagingType, true)) { |
112
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
113
|
1 |
|
'messagingType must be either %s.', |
114
|
1 |
|
implode(', ', $allowedMessagingType) |
115
|
|
|
)); |
116
|
|
|
} |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
9 |
|
private function getAllowedOptionsKeys(): array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
9 |
|
self::OPTION_MESSAGING_TYPE, |
126
|
9 |
|
self::OPTION_NOTIFICATION_TYPE, |
127
|
9 |
|
self::OPTION_TAG, |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
2 |
|
public function getAllowedMessagingType(): array |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
2 |
|
self::MESSAGING_TYPE_RESPONSE, |
138
|
2 |
|
self::MESSAGING_TYPE_MESSAGE_TAG, |
139
|
2 |
|
self::MESSAGING_TYPE_NON_PROMOTIONAL_SUBSCRIPTION, |
140
|
2 |
|
self::MESSAGING_TYPE_UPDATE, |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|