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