Completed
Branch master (c30bd6)
by Romain
02:08
created

Broadcast::isValidOptions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 14.6179

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 15
ccs 3
cts 11
cp 0.2727
rs 9.6111
cc 5
nc 5
nop 1
crap 14.6179
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
     * @return \Kerox\Messenger\Response\BroadcastResponse
26
     */
27 1
    public function create($message): BroadcastResponse
28
    {
29 1
        $message = $this->isValidMessage($message);
30
31 1
        $request = new BroadcastRequest($this->pageToken, $message);
32 1
        $response = $this->client->post('me/message_creatives', $request->build());
33
34 1
        return new BroadcastResponse($response);
35
    }
36
37
    /**
38
     * @param string $messageCreativeId
39
     * @param array  $options
40
     *
41
     * @throws \Kerox\Messenger\Exception\MessengerException
42
     *
43
     * @return \Kerox\Messenger\Response\BroadcastResponse
44
     */
45 1
    public function send(string $messageCreativeId, array $options = []): BroadcastResponse
46
    {
47 1
        $this->isValidOptions($options);
48
49 1
        $request = new BroadcastRequest($this->pageToken, null, $messageCreativeId, $options);
50 1
        $response = $this->client->post('me/broadcast_messages', $request->build());
51
52 1
        return new BroadcastResponse($response);
53
    }
54
55
    /**
56
     * @param array $options
57
     *
58
     * @throws \Kerox\Messenger\Exception\MessengerException
59
     */
60 1
    private function isValidOptions(array $options): void
61
    {
62 1
        $allowedOptionsKeys = $this->getAllowedOptionsKeys();
63 1
        foreach ($options as $key => $value) {
64
            if (!\in_array($key, $allowedOptionsKeys, true)) {
65
                throw new InvalidOptionException(sprintf(
66
                    'Only "%s" are allowed keys for options.',
67
                    implode(', ', $allowedOptionsKeys)
68
                ));
69
            }
70
71
            if ($key === self::OPTION_NOTIFICATION_TYPE) {
72
                $this->isValidNotificationType($value);
73
            } elseif ($key === self::OPTION_TAG) {
74
                $this->isValidTag($value);
75
            }
76
        }
77 1
    }
78
79
    /**
80
     * @return array
81
     */
82 1
    private function getAllowedOptionsKeys(): array
83
    {
84
        return [
85 1
            self::OPTION_NOTIFICATION_TYPE,
86 1
            self::OPTION_TAG,
87
        ];
88
    }
89
}
90