Passed
Pull Request — master (#32)
by Romain
03:48
created

Send::action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\ClientInterface;
5
use Kerox\Messenger\Model\Message;
6
use Kerox\Messenger\Model\Message\Attachment;
7
use Kerox\Messenger\Request\SendRequest;
8
use Kerox\Messenger\Response\SendResponse;
9
10
class Send extends AbstractApi
11
{
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
    /**
22
     * Send constructor.
23
     *
24
     * @param string $pageToken
25
     * @param \GuzzleHttp\ClientInterface $client
26
     */
27 9
    public function __construct(string $pageToken, ClientInterface $client)
28
    {
29 9
        parent::__construct($pageToken, $client);
30 9
    }
31
32
    /**
33
     * @param string $recipient
34
     * @param $message
35
     * @param string $notificationType
36
     * @return \Kerox\Messenger\Response\SendResponse
37
     */
38 5
    public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
39
    {
40 5
        $message = $this->isValidMessage($message);
41 4
        $this->isValidNotificationType($notificationType);
42
43 3
        $request = new SendRequest($this->pageToken, $message, $recipient, $notificationType);
44 3
        $response = $this->client->post('me/messages', $request->build());
45
46 3
        return new SendResponse($response);
47
    }
48
49
    /**
50
     * @deprecated since 1.2.0 and will be remove in 1.3.0. Use the method `message()` instead
51
     * @see message()
52
     * @param string $recipient
53
     * @param $message
54
     * @param string $notificationType
55
     * @return \Kerox\Messenger\Response\SendResponse
56
     */
57
    public function sendMessage(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
58
    {
59
        return $this->message($recipient, $message, $notificationType);
60
    }
61
62
    /**
63
     * @param string $recipient
64
     * @param string $action
65
     * @param string $notificationType
66
     * @return \Kerox\Messenger\Response\SendResponse
67
     */
68 2
    public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
69
    {
70 2
        $this->isValidAction($action);
71 1
        $this->isValidNotificationType($notificationType);
72
73 1
        $request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, SendRequest::TYPE_ACTION);
74 1
        $response = $this->client->post('me/messages', $request->build());
75
76 1
        return new SendResponse($response);
77
    }
78
79
    /**
80
     * @deprecated since 1.2.0 and will be removed in 1.3.0. Use the method `action()` instead
81
     * @see action()
82
     * @param string $recipient
83
     * @param string $action
84
     * @param string $notificationType
85
     * @return \Kerox\Messenger\Response\SendResponse
86
     */
87
    public function sendAction(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
88
    {
89
        return $this->action($recipient, $action, $notificationType);
90
    }
91
92
    /**
93
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
94
     * @return \Kerox\Messenger\Response\SendResponse
95
     */
96 1
    public function attachment(Attachment $attachment): SendResponse
97
    {
98 1
        $message = $this->isValidMessage($attachment);
99
100 1
        $request = new SendRequest($this->pageToken, $message);
101 1
        $response = $this->client->post('me/message_attachments', $request->build());
102
103 1
        return new SendResponse($response);
104
    }
105
106
    /**
107
     * @param $message
108
     * @return \Kerox\Messenger\Model\Message
109
     * @throws \InvalidArgumentException
110
     */
111 6
    private function isValidMessage($message): Message
112
    {
113 6
        if ($message instanceof Message) {
114 1
            return $message;
115
        }
116
117 5
        if (is_string($message) || $message instanceof Attachment) {
118 4
            return new Message($message);
119
        }
120
121 1
        throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment');
122
    }
123
124
    /**
125
     * @param string $notificationType
126
     */
127 5
    private function isValidNotificationType(string $notificationType)
128
    {
129 5
        $allowedNotificationType = $this->getAllowedNotificationType();
130 5
        if (!in_array($notificationType, $allowedNotificationType)) {
131 1
            throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType));
132
        }
133 4
    }
134
135
    /**
136
     * @return array
137
     */
138 5
    private function getAllowedNotificationType(): array
139
    {
140
        return [
141 5
            self::NOTIFICATION_TYPE_REGULAR,
142 5
            self::NOTIFICATION_TYPE_SILENT_PUSH,
143 5
            self::NOTIFICATION_TYPE_NO_PUSH,
144
        ];
145
    }
146
147
    /**
148
     * @param string $action
149
     */
150 2
    private function isValidAction(string $action)
151
    {
152 2
        $allowedSenderAction = $this->getAllowedSenderAction();
153 2
        if (!in_array($action, $allowedSenderAction)) {
154 1
            throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction));
155
        }
156 1
    }
157
158
    /**
159
     * @return array
160
     */
161 2
    private function getAllowedSenderAction(): array
162
    {
163
        return [
164 2
            self::SENDER_ACTION_TYPING_ON,
165 2
            self::SENDER_ACTION_TYPING_OFF,
166 2
            self::SENDER_ACTION_MARK_SEEN,
167
        ];
168
    }
169
}
170