Completed
Pull Request — master (#34)
by Romain
02:42
created

Send::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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