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