Passed
Pull Request — master (#76)
by
unknown
07:15
created

Send::message()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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