Completed
Push — master ( 9609d8...b3280e )
by Janusz
06:06
created

SmslabsClient::setFlashMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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