Completed
Pull Request — master (#127)
by Alexandr
05:03
created

Broadcast::isValidOptions()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 19.1203

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
ccs 4
cts 14
cp 0.2857
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 19.1203
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
class Broadcast extends AbstractApi implements SendInterface
14
{
15
    use ValidatorTrait;
16
17
    /**
18
     * @param string|\Kerox\Messenger\Model\Message $message
19
     *
20
     * @throws \Psr\Http\Client\ClientExceptionInterface
21
     * @throws \Kerox\Messenger\Exception\MessengerException
22
     *
23
     * @return \Kerox\Messenger\Response\BroadcastResponse
24
     */
25 1
    public function create($message): BroadcastResponse
26
    {
27 1
        $message = $this->isValidMessage($message);
28
29 1
        $request = new BroadcastRequest('me/message_creatives', $message);
30 1
        $response = $this->client->sendRequest($request->build());
31
32 1
        return new BroadcastResponse($response);
33
    }
34
35
    /**
36
     * @param string $messageCreativeId
37
     * @param array  $options
38
     *
39
     * @throws \Kerox\Messenger\Exception\MessengerException
40
     * @throws \Psr\Http\Client\ClientExceptionInterface
41
     *
42
     * @return \Kerox\Messenger\Response\BroadcastResponse
43
     */
44 1
    public function send(string $messageCreativeId, array $options = []): BroadcastResponse
45
    {
46 1
        $options = $this->isValidOptions($options);
47
48 1
        $request = new BroadcastRequest('me/broadcast_messages', null, $messageCreativeId, $options);
49 1
        $response = $this->client->sendRequest($request->build());
50
51 1
        return new BroadcastResponse($response);
52
    }
53
54
    /**
55
     * @param array $options
56
     *
57
     * @throws \Kerox\Messenger\Exception\MessengerException
58
     *
59
     * @return array
60
     */
61 1
    private function isValidOptions(array $options): array
62
    {
63 1
        $allowedOptionsKeys = $this->getAllowedOptionsKeys();
64 1
        foreach ($options as $key => $value) {
65
            if (!\in_array($key, $allowedOptionsKeys, true)) {
66
                throw new InvalidOptionException(sprintf(
67
                    'Only "%s" are allowed keys for options.',
68
                    implode(', ', $allowedOptionsKeys)
69
                ));
70
            }
71
72
            if ($key === self::OPTION_MESSAGING_TYPE) {
73
                $this->isValidMessagingType($value);
74
            } elseif ($key === self::OPTION_NOTIFICATION_TYPE) {
75
                $this->isValidNotificationType($value);
76
            } elseif ($key === self::OPTION_TAG) {
77
                $this->isValidTag($value);
78
            }
79
        }
80
81 1
        return $options;
82
    }
83
84
    /**
85
     * @return array
86
     */
87 1
    private function getAllowedOptionsKeys(): array
88
    {
89
        return [
90 1
            self::OPTION_MESSAGING_TYPE,
91 1
            self::OPTION_NOTIFICATION_TYPE,
92 1
            self::OPTION_TAG,
93
        ];
94
    }
95
}
96