|
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\Request\BroadcastRequest; |
|
9
|
|
|
use Kerox\Messenger\Response\BroadcastResponse; |
|
10
|
|
|
use Kerox\Messenger\SendInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Broadcast extends AbstractApi implements SendInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use ValidatorTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string|\Kerox\Messenger\Model\Message $message |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \Exception |
|
20
|
|
|
* |
|
21
|
|
|
* @return \Kerox\Messenger\Response\BroadcastResponse |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function create($message): BroadcastResponse |
|
24
|
|
|
{ |
|
25
|
1 |
|
$message = $this->isValidMessage($message); |
|
26
|
|
|
|
|
27
|
1 |
|
$request = new BroadcastRequest($this->pageToken, $message); |
|
28
|
1 |
|
$response = $this->client->post('me/message_creatives', $request->build()); |
|
29
|
|
|
|
|
30
|
1 |
|
return new BroadcastResponse($response); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $messageCreativeId |
|
35
|
|
|
* @param array $options |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Kerox\Messenger\Response\BroadcastResponse |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function send(string $messageCreativeId, array $options = []): BroadcastResponse |
|
40
|
|
|
{ |
|
41
|
1 |
|
$options = $this->isValidOptions($options); |
|
42
|
|
|
|
|
43
|
1 |
|
$request = new BroadcastRequest($this->pageToken, null, $messageCreativeId, $options); |
|
44
|
1 |
|
$response = $this->client->post('me/broadcast_messages', $request->build()); |
|
45
|
|
|
|
|
46
|
1 |
|
return new BroadcastResponse($response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
1 |
|
private function isValidOptions(array $options): array |
|
55
|
|
|
{ |
|
56
|
1 |
|
$allowedOptionsKeys = $this->getAllowedOptionsKeys(); |
|
57
|
1 |
|
foreach ($options as $key => $value) { |
|
58
|
|
|
if (!\in_array($key, $allowedOptionsKeys, true)) { |
|
59
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
60
|
|
|
'Only %s are allowed keys for options.', |
|
61
|
|
|
implode(', ', $allowedOptionsKeys) |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($key === self::OPTION_NOTIFICATION_TYPE) { |
|
66
|
|
|
$this->isValidNotificationType($value); |
|
67
|
|
|
} elseif ($key === self::OPTION_TAG) { |
|
68
|
|
|
$this->isValidTag($value); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $options; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function getAllowedOptionsKeys(): array |
|
79
|
|
|
{ |
|
80
|
|
|
return [ |
|
81
|
1 |
|
self::OPTION_NOTIFICATION_TYPE, |
|
82
|
1 |
|
self::OPTION_TAG, |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|