Completed
Push — master ( 8e0ded...b5dffc )
by Romain
10s
created

Broadcast   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A getInstance() 0 7 2
A getAllowedNotificationType() 0 6 1
A getAllowedTag() 0 14 1
A send() 0 15 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
     * @var null|\Kerox\Messenger\Api\Broadcast
18
     */
19
    private static $_instance;
20
21
    /**
22
     * @param string                      $pageToken
23
     * @param \GuzzleHttp\ClientInterface $client
24
     *
25
     * @return \Kerox\Messenger\Api\Broadcast
26
     */
27
    public static function getInstance(string $pageToken, ClientInterface $client): self
28
    {
29
        if (self::$_instance === null) {
30
            self::$_instance = new self($pageToken, $client);
31
        }
32
33
        return self::$_instance;
34
    }
35
36
    /**
37
     * @param string|\Kerox\Messenger\Model\Message $message
38
     *
39
     * @throws \Exception
40
     *
41
     * @return \Kerox\Messenger\Response\BroadcastResponse
42
     */
43
    public function create($message): BroadcastResponse
44
    {
45
        $message = $this->isValidMessage($message);
46
47
        $request = new BroadcastRequest($this->pageToken, $message);
48
        $response = $this->client->post('me/message_creatives', $request->build());
49
50
        return new BroadcastResponse($response);
51
    }
52
53
    /**
54
     * @param string      $messageCreativeId
55
     * @param string      $notificationType
56
     * @param string|null $tag
57
     *
58
     * @throws \InvalidArgumentException
59
     *
60
     * @return \Kerox\Messenger\Response\BroadcastResponse
61
     */
62
    public function send(
63
        string $messageCreativeId,
64
        string $notificationType = Send::NOTIFICATION_TYPE_REGULAR,
65
        ?string $tag = null
66
    ): BroadcastResponse {
67
        $this->isValidNotificationType($notificationType, $this->getAllowedNotificationType());
68
69
        if ($tag !== null) {
70
            $this->isValidTag($tag, $this->getAllowedTag());
71
        }
72
73
        $request = new BroadcastRequest($this->pageToken, null, $messageCreativeId, $notificationType, $tag);
74
        $response = $this->client->post('me/broadcast_messages', $request->build());
75
76
        return new BroadcastResponse($response);
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    private function getAllowedNotificationType(): array
83
    {
84
        return [
85
            Send::NOTIFICATION_TYPE_REGULAR,
86
            Send::NOTIFICATION_TYPE_SILENT_PUSH,
87
            Send::NOTIFICATION_TYPE_NO_PUSH,
88
        ];
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    private function getAllowedTag(): array
95
    {
96
        return [
97
            Send::TAG_ISSUE_RESOLUTION,
98
            Send::TAG_RESERVATION_UPDATE,
99
            Send::TAG_SHIPPING_UPDATE,
100
            Send::TAG_APPOINTMENT_UPDATE,
101
            Send::TAG_GAME_EVENT,
102
            Send::TAG_TRANSPORTATION_UPDATE,
103
            Send::TAG_FEATURE_FUNCTIONALITY_UPDATE,
104
            Send::TAG_TICKET_UPDATE,
105
            Send::TAG_ACCOUNT_UPDATE,
106
            Send::TAG_PAYMENT_UPDATE,
107
            Send::TAG_PERSONAL_FINANCE_UPDATE,
108
        ];
109
    }
110
}
111