Passed
Push — master ( f1ff91...53e7cc )
by Romain
59s
created

Send   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.55%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 198
ccs 49
cts 64
cp 0.7655
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 8 2
A message() 0 14 2
A action() 0 10 1
A attachment() 0 9 1
A isValidMessage() 0 12 4
A isValidNotificationType() 0 7 2
A getAllowedNotificationType() 0 8 1
A isValidAction() 0 7 2
A getAllowedSenderAction() 0 8 1
A isValidTag() 0 7 2
A getAllowedTag() 0 13 1
1
<?php
2
3
namespace Kerox\Messenger\Api;
4
5
use GuzzleHttp\ClientInterface;
6
use Kerox\Messenger\Model\Message;
7
use Kerox\Messenger\Model\Message\Attachment;
8
use Kerox\Messenger\Request\SendRequest;
9
use Kerox\Messenger\Response\SendResponse;
10
11
class Send extends AbstractApi
12
{
13
14
    const SENDER_ACTION_TYPING_ON = 'typing_on';
15
    const SENDER_ACTION_TYPING_OFF = 'typing_off';
16
    const SENDER_ACTION_MARK_SEEN = 'mark_seen';
17
18
    const NOTIFICATION_TYPE_REGULAR = 'REGULAR';
19
    const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH';
20
    const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH';
21
22
    const TAG_SHIPPING_UPDATE = 'SHIPPING_UPDATE';
23
    const TAG_RESERVATION_UPDATE = 'RESERVATION_UPDATE';
24
    const TAG_ISSUE_RESOLUTION = 'ISSUE_RESOLUTION';
25
    const TAG_APPOINTMENT_UPDATE = 'APPOINTMENT_UPDATE';
26
    const TAG_GAME_EVENT = 'GAME_EVENT';
27
    const TAG_TRANSPORTATION_UPDATE = 'TRANSPORTATION_UPDATE';
28
    const TAG_FEATURE_FUNCTIONALITY_UPDATE = 'FEATURE_FUNCTIONALITY_UPDATE';
29
    const TAG_TICKET_UPDATE = 'TICKET_UPDATE';
30
31
    /**
32
     * @var null|\Kerox\Messenger\Api\Send
33
     */
34
    private static $_instance;
35
36
    /**
37
     * Send constructor.
38
     *
39
     * @param string $pageToken
40
     * @param \GuzzleHttp\ClientInterface $client
41
     */
42 9
    public function __construct(string $pageToken, ClientInterface $client)
43
    {
44 9
        parent::__construct($pageToken, $client);
45 9
    }
46
47
    /**
48
     * @param string $pageToken
49
     * @param \GuzzleHttp\ClientInterface $client
50
     * @return \Kerox\Messenger\Api\Send
51
     */
52 1
    public static function getInstance(string $pageToken, ClientInterface $client): Send
53
    {
54 1
        if (self::$_instance === null) {
55 1
            self::$_instance = new Send($pageToken, $client);
56
        }
57
58 1
        return self::$_instance;
59
    }
60
61
    /**
62
     * @param string $recipient
63
     * @param string|\Kerox\Messenger\Model\Message $message
64
     * @param string $notificationType
65
     * @param string|null $tag
66
     * @return \Kerox\Messenger\Response\SendResponse
67
     */
68 5
    public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR, $tag = null): SendResponse
69
    {
70 5
        $message = $this->isValidMessage($message);
71 4
        $this->isValidNotificationType($notificationType);
72
73 3
        if ($tag !== null) {
74
            $this->isValidTag($tag);
75
        }
76
77 3
        $request = new SendRequest($this->pageToken, $message, $recipient, $notificationType, $tag);
78 3
        $response = $this->client->post('me/messages', $request->build());
79
80 3
        return new SendResponse($response);
81
    }
82
83
    /**
84
     * @param string $recipient
85
     * @param string $action
86
     * @param string $notificationType
87
     * @return \Kerox\Messenger\Response\SendResponse
88
     */
89 2
    public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
90
    {
91 2
        $this->isValidAction($action);
92 1
        $this->isValidNotificationType($notificationType);
93
94 1
        $request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, SendRequest::TYPE_ACTION);
95 1
        $response = $this->client->post('me/messages', $request->build());
96
97 1
        return new SendResponse($response);
98
    }
99
100
    /**
101
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
102
     * @return \Kerox\Messenger\Response\SendResponse
103
     */
104 1
    public function attachment(Attachment $attachment): SendResponse
105
    {
106 1
        $message = $this->isValidMessage($attachment);
107
108 1
        $request = new SendRequest($this->pageToken, $message);
109 1
        $response = $this->client->post('me/message_attachments', $request->build());
110
111 1
        return new SendResponse($response);
112
    }
113
114
    /**
115
     * @param $message
116
     * @return \Kerox\Messenger\Model\Message
117
     * @throws \InvalidArgumentException
118
     */
119 6
    private function isValidMessage($message): Message
120
    {
121 6
        if ($message instanceof Message) {
122 1
            return $message;
123
        }
124
125 5
        if (is_string($message) || $message instanceof Attachment) {
126 4
            return new Message($message);
127
        }
128
129 1
        throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment');
130
    }
131
132
    /**
133
     * @param string $notificationType
134
     * @throws \InvalidArgumentException
135
     */
136 5
    private function isValidNotificationType(string $notificationType)
137
    {
138 5
        $allowedNotificationType = $this->getAllowedNotificationType();
139 5
        if (!in_array($notificationType, $allowedNotificationType)) {
140 1
            throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType));
141
        }
142 4
    }
143
144
    /**
145
     * @return array
146
     */
147 5
    private function getAllowedNotificationType(): array
148
    {
149
        return [
150 5
            self::NOTIFICATION_TYPE_REGULAR,
151 5
            self::NOTIFICATION_TYPE_SILENT_PUSH,
152 5
            self::NOTIFICATION_TYPE_NO_PUSH,
153
        ];
154
    }
155
156
    /**
157
     * @param string $action
158
     * @throws \InvalidArgumentException
159
     */
160 2
    private function isValidAction(string $action)
161
    {
162 2
        $allowedSenderAction = $this->getAllowedSenderAction();
163 2
        if (!in_array($action, $allowedSenderAction)) {
164 1
            throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction));
165
        }
166 1
    }
167
168
    /**
169
     * @return array
170
     */
171 2
    private function getAllowedSenderAction(): array
172
    {
173
        return [
174 2
            self::SENDER_ACTION_TYPING_ON,
175 2
            self::SENDER_ACTION_TYPING_OFF,
176 2
            self::SENDER_ACTION_MARK_SEEN,
177
        ];
178
    }
179
180
    /**
181
     * @param string $tag
182
     * @throws \InvalidArgumentException
183
     */
184
    private function isValidTag(string $tag)
185
    {
186
        $allowedTag = $this->getAllowedTag();
187
        if (!in_array($tag, $allowedTag)) {
188
            throw new \InvalidArgumentException('$tag must be either ' . implode(', ', $allowedTag));
189
        }
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    private function getAllowedTag(): array
196
    {
197
        return [
198
            self::TAG_ISSUE_RESOLUTION,
199
            self::TAG_RESERVATION_UPDATE,
200
            self::TAG_SHIPPING_UPDATE,
201
            self::TAG_APPOINTMENT_UPDATE,
202
            self::TAG_GAME_EVENT,
203
            self::TAG_TRANSPORTATION_UPDATE,
204
            self::TAG_FEATURE_FUNCTIONALITY_UPDATE,
205
            self::TAG_TICKET_UPDATE,
206
        ];
207
    }
208
}
209