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\Request\BroadcastRequest; |
10
|
|
|
use Kerox\Messenger\Response\BroadcastResponse; |
11
|
|
|
use Kerox\Messenger\SendInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @deprecated Since version 3.2.0 and will be removed in version 4.0.0. |
15
|
|
|
*/ |
16
|
|
|
class Broadcast extends AbstractApi implements SendInterface |
17
|
|
|
{ |
18
|
|
|
use ValidatorTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string|\Kerox\Messenger\Model\Message $message |
22
|
|
|
* |
23
|
|
|
* @throws \Exception |
24
|
|
|
*/ |
25
|
1 |
View Code Duplication |
public function create($message): BroadcastResponse |
|
|
|
|
26
|
|
|
{ |
27
|
1 |
|
$message = $this->isValidMessage($message); |
28
|
|
|
|
29
|
1 |
|
$request = new BroadcastRequest($this->pageToken, $message); |
|
|
|
|
30
|
1 |
|
$response = $this->client->post('me/message_creatives', $request->build()); |
31
|
|
|
|
32
|
1 |
|
return new BroadcastResponse($response); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
37
|
|
|
*/ |
38
|
1 |
View Code Duplication |
public function send(string $messageCreativeId, array $options = []): BroadcastResponse |
|
|
|
|
39
|
|
|
{ |
40
|
1 |
|
$this->isValidOptions($options); |
41
|
|
|
|
42
|
1 |
|
$request = new BroadcastRequest($this->pageToken, null, $messageCreativeId, $options); |
|
|
|
|
43
|
1 |
|
$response = $this->client->post('me/broadcast_messages', $request->build()); |
44
|
|
|
|
45
|
1 |
|
return new BroadcastResponse($response); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
50
|
|
|
*/ |
51
|
1 |
|
private function isValidOptions(array $options): void |
52
|
|
|
{ |
53
|
1 |
|
$allowedOptionsKeys = $this->getAllowedOptionsKeys(); |
54
|
1 |
|
foreach ($options as $key => $value) { |
55
|
|
View Code Duplication |
if (!\in_array($key, $allowedOptionsKeys, true)) { |
|
|
|
|
56
|
|
|
throw new InvalidOptionException(sprintf('Only "%s" are allowed keys for options.', implode(', ', $allowedOptionsKeys))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($key === self::OPTION_NOTIFICATION_TYPE) { |
60
|
|
|
$this->isValidNotificationType($value); |
61
|
|
|
} elseif ($key === self::OPTION_TAG) { |
62
|
|
|
$this->isValidTag($value); |
63
|
|
|
} |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
private function getAllowedOptionsKeys(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
1 |
|
self::OPTION_NOTIFICATION_TYPE, |
71
|
1 |
|
self::OPTION_TAG, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.