Completed
Push — master ( 4c1222...393226 )
by Romain
10s
created

Broadcast::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
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
11
class Broadcast extends AbstractApi
12
{
13
    use ValidatorTrait;
14
15
    /**
16
     * @param string|\Kerox\Messenger\Model\Message $message
17
     *
18
     * @throws \Exception
19
     *
20
     * @return \Kerox\Messenger\Response\BroadcastResponse
21
     */
22
    public function create($message): BroadcastResponse
23
    {
24
        $message = $this->isValidMessage($message);
25
26
        $request = new BroadcastRequest($this->pageToken, $message);
27
        $response = $this->client->post('me/message_creatives', $request->build());
28
29
        return new BroadcastResponse($response);
30
    }
31
32
    /**
33
     * @param string      $messageCreativeId
34
     * @param string      $notificationType
35
     * @param string|null $tag
36
     *
37
     * @throws \InvalidArgumentException
38
     *
39
     * @return \Kerox\Messenger\Response\BroadcastResponse
40
     */
41
    public function send(
42
        string $messageCreativeId,
43
        string $notificationType = Send::NOTIFICATION_TYPE_REGULAR,
44
        ?string $tag = null
45
    ): BroadcastResponse {
46
        $this->isValidNotificationType($notificationType, $this->getAllowedNotificationType());
47
48
        if ($tag !== null) {
49
            $this->isValidTag($tag, $this->getAllowedTag());
50
        }
51
52
        $request = new BroadcastRequest($this->pageToken, null, $messageCreativeId, $notificationType, $tag);
53
        $response = $this->client->post('me/broadcast_messages', $request->build());
54
55
        return new BroadcastResponse($response);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    private function getAllowedNotificationType(): array
62
    {
63
        return [
64
            Send::NOTIFICATION_TYPE_REGULAR,
65
            Send::NOTIFICATION_TYPE_SILENT_PUSH,
66
            Send::NOTIFICATION_TYPE_NO_PUSH,
67
        ];
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    private function getAllowedTag(): array
74
    {
75
        return [
76
            Send::TAG_ISSUE_RESOLUTION,
77
            Send::TAG_RESERVATION_UPDATE,
78
            Send::TAG_SHIPPING_UPDATE,
79
            Send::TAG_APPOINTMENT_UPDATE,
80
            Send::TAG_GAME_EVENT,
81
            Send::TAG_TRANSPORTATION_UPDATE,
82
            Send::TAG_FEATURE_FUNCTIONALITY_UPDATE,
83
            Send::TAG_TICKET_UPDATE,
84
            Send::TAG_ACCOUNT_UPDATE,
85
            Send::TAG_PAYMENT_UPDATE,
86
            Send::TAG_PERSONAL_FINANCE_UPDATE,
87
        ];
88
    }
89
}
90