SmslabsClient::getSmsQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Ittoolspl\Smslabs;
5
6
use Ittoolspl\Smslabs\VO\AccountBalance;
7
use Ittoolspl\Smslabs\VO\Sender;
8
use Ittoolspl\Smslabs\VO\SmsDetails;
9
use Ittoolspl\Smslabs\VO\SmsIn;
10
use Ittoolspl\Smslabs\VO\SmsOut;
11
use Ittoolspl\Smslabs\VO\SmsSentResponse;
12
use Ittoolspl\Smslabs\Exception\EmptySMSQueueException;
13
14
class SmslabsClient
15
{
16
    const SEND_SMS_URL = '/sendSms';
17
    const SENDERS_URL = '/senders';
18
    const SMS_STATUS_URL = '/smsStatus';
19
    const SMS_LIST_URL = '/sms';
20
    const SMS_IN_URL = '/smsIn';
21
    const ACCOUNT_URL = '/account';
22
23
    /**
24
     * @var HttpClient
25
     */
26
    private $client;
27
28
    /**
29
     * @var int
30
     */
31
    private $isFlashMessage = 0;
32
33
    /**
34
     * @var string
35
     */
36
    private $senderId;
37
38
    /**
39
     * @var int
40
     */
41
    private $expirationMinutes = 0;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    private $sendDateTime;
47
48
    /**
49
     * @var array[]
50
     */
51
    private $smsToSend = [];
52
53
    /**
54
     * @var SmsSentResponse[]
55
     */
56
    private $smsStatus = [];
57
58
    /**
59
     * Smslabs constructor.
60
     * @param string $appKey
61
     * @param string $secretKey
62
     */
63 18
    public function __construct(string $appKey, string $secretKey)
64
    {
65 18
        $this->client = new HttpClient($appKey, $secretKey);
66 18
    }
67
68
    /**
69
     * @return HttpClient
70
     */
71 1
    public function getClient() : HttpClient
72
    {
73 1
        return $this->client;
74
    }
75
76
    /**
77
     * @param HttpClient $client
78
     * @return SmslabsClient $this
79
     */
80 7
    public function setClient(HttpClient $client) : SmslabsClient
81
    {
82 7
        $this->client = $client;
83
84 7
        return $this;
85
    }
86
87
    /**
88
     * @return int
89
     */
90 1
    public function isFlashMessage() : int
91
    {
92 1
        return $this->isFlashMessage;
93
    }
94
95
    /**
96
     * @param int $isFlashMessage
97
     * @return SmslabsClient $this
98
     */
99 2
    public function setFlashMessage(int $isFlashMessage) : SmslabsClient
100
    {
101 2
        $this->isFlashMessage = $isFlashMessage;
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 1
    public function getSenderId() : string
110
    {
111 1
        return $this->senderId;
112
    }
113
114
    /**
115
     * @param string $senderId
116
     * @return SmslabsClient $this
117
     */
118 6
    public function setSenderId(string $senderId) : SmslabsClient
119
    {
120 6
        $this->senderId = $senderId;
121
122 6
        return $this;
123
    }
124
125
    /**
126
     * @return int
127
     */
128 3
    public function getExpirationMinutes() : int
129
    {
130 3
        return $this->expirationMinutes;
131
    }
132
133
    /**
134
     * @param int $expirationMinutes
135
     * @return SmslabsClient $this
136
     */
137 6
    public function setExpirationMinutes(int $expirationMinutes) : SmslabsClient
138
    {
139 6
        if ($expirationMinutes < 1 || $expirationMinutes > 5520) {
140 2
            throw new \InvalidArgumentException('Valid values: 1 - 5520');
141
        }
142
143 4
        $this->expirationMinutes = $expirationMinutes;
144
145 4
        return $this;
146
    }
147
148
    /**
149
     * @return \DateTime
150
     */
151 1
    public function getSendDateTime() : \DateTime
152
    {
153 1
        return $this->sendDateTime;
154
    }
155
156
    /**
157
     * @param \DateTime $sendDateTime
158
     * @return SmslabsClient $this
159
     */
160 2
    public function setSendDateTime(\DateTime $sendDateTime) : SmslabsClient
161
    {
162 2
        $this->sendDateTime = $sendDateTime;
163
164 2
        return $this;
165
    }
166
167
    /**
168
     * @param string $phoneNumber
169
     * @param string $message
170
     * @param bool $isFlashMessage
171
     * @param int $expirationMinutes
172
     * @param \DateTime $sendDateTime
173
     * @return SmslabsClient $this
174
     */
175 6
    public function add(
176
        string $phoneNumber,
177
        string $message,
178
        bool $isFlashMessage = null,
179
        int $expirationMinutes = null,
180
        \DateTime $sendDateTime = null
181
    ) : SmslabsClient {
182 6
        if (!$this->checkPhoneNumber($phoneNumber)) {
183 1
            throw new \InvalidArgumentException('Invalid phone number');
184
        }
185
186
        $sms = [
187 5
            'phone_number' => $phoneNumber,
188 5
            'message' => $message,
189 5
            'flash' => $isFlashMessage === null ? $this->isFlashMessage : $isFlashMessage,
190 5
            'expiration' => $expirationMinutes === null ? $this->expirationMinutes : $expirationMinutes,
191 5
            'sender_id' => $this->senderId,
192
        ];
193
194 5
        if ($sendDateTime instanceof \DateTime) {
195 1
            $sms['send_date'] = $sendDateTime->getTimestamp();
196 5
        } elseif ($this->sendDateTime instanceof \DateTime) {
197 1
            $sms['send_date'] = $this->sendDateTime->getTimestamp();
198
        }
199
200 5
        $this->smsToSend[] = $sms;
201
202 5
        return $this;
203
    }
204
205
    /**
206
     * @param string $phoneNumber
207
     * @return bool
208
     */
209 6
    private function checkPhoneNumber(string $phoneNumber) : bool
210
    {
211 6
        return (bool)preg_match('/^\+[0-9]{10,13}$/', $phoneNumber);
212
    }
213
214
    /**
215
     * @return SmslabsClient $this
216
     */
217 3
    public function send() : SmslabsClient
218
    {
219 3
        if (empty($this->smsToSend)) {
220 1
            throw new EmptySMSQueueException('No messages to send');
221
        }
222
223 2
        if ($this->senderId === null) {
224 1
            throw new \InvalidArgumentException('SenderId is missing');
225
        }
226
227 1
        foreach ($this->smsToSend as $key => $sms) {
228 1
            $httpResponse = $this->client->sendRequest(self::SEND_SMS_URL, $sms, 'PUT');
229
230 1
            $this->smsStatus[] = new SmsSentResponse($httpResponse['account'], $httpResponse['sms_id']);
231
232 1
            unset($this->smsToSend[$key]);
233
        }
234
235 1
        return $this;
236
    }
237
238
    /**
239
     * @return SmsSentResponse[]
240
     */
241 1
    public function getSentStatus() : array
242
    {
243 1
        return $this->smsStatus;
244
    }
245
246
    /**
247
     * @return Sender[]
248
     */
249 1
    public function getAvailableSenders() : array
250
    {
251 1
        $sendersResponse = $this->client->sendRequest(self::SENDERS_URL);
252
253 1
        $list = [];
254
255 1
        foreach ($sendersResponse as $sender) {
256 1
            $list[] = Sender::createFromResponseArray($sender);
257
        }
258
259 1
        return $list;
260
    }
261
262
    /**
263
     * @return AccountBalance
264
     */
265 1
    public function getAccountBalance() : AccountBalance
266
    {
267 1
        $balanceResponse = $this->client->sendRequest(self::ACCOUNT_URL);
268
269 1
        return new AccountBalance($balanceResponse['account'] / 100);
270
    }
271
272
    /**
273
     * @return SmsIn[]
274
     */
275 1
    public function getSmsIn() : array
276
    {
277 1
        $smsInResponse = $this->client->sendRequest(self::SMS_IN_URL);
278
279 1
        $list = [];
280
281 1
        foreach ($smsInResponse as $smsIn) {
282 1
            $list[] = SmsIn::createFromResponseArray($smsIn);
283
        }
284
285 1
        return $list;
286
    }
287
288
    /**
289
     * @param int $offset
290
     * @param int $limit
291
     * @return SmsOut[]
292
     */
293 1
    public function getSmsOut(int $offset = 0, int $limit = 100) : array
294
    {
295 1
        $smsOutResponse = $this->client->sendRequest(self::SMS_LIST_URL . '?offset=' . $offset . '&limit=' . $limit);
296
297 1
        $list = [];
298
299 1
        foreach ($smsOutResponse as $smsOut) {
300 1
            $list[] = SmsOut::createFromResponseArray($smsOut);
301
        }
302
303 1
        return $list;
304
    }
305
306
    /**
307
     * @param string $smsId
308
     * @return bool
309
     */
310 2
    private function isValidSmsId(string $smsId) : bool
311
    {
312 2
        return (bool) preg_match('/[0-9a-f]{24}/', $smsId);
313
    }
314
315
    /**
316
     * @param string $smsId
317
     * @return SmsDetails
318
     */
319 2
    public function getSmsDetails(string $smsId) : SmsDetails
320
    {
321 2
        if (!$this->isValidSmsId($smsId)) {
322 1
            throw new \InvalidArgumentException('Invalid SMS ID');
323
        }
324
325 1
        $detailsResponse = $this->client->sendRequest(self::SMS_STATUS_URL . '?id=' . $smsId);
326
327 1
        $smsDetails = SmsDetails::createFromResponseArray($detailsResponse);
328
329 1
        return $smsDetails;
330
    }
331
332
    /**
333
     * @return array[]
334
     */
335 3
    public function getSmsQueue() : array
336
    {
337 3
        return $this->smsToSend;
338
    }
339
}
340