Completed
Pull Request — master (#140)
by
unknown
02:19
created

Broadcast::isValidOptions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 12.4085

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 3
cts 9
cp 0.3333
rs 9.6111
cc 5
nc 5
nop 1
crap 12.4085
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
    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
    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
            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