Completed
Push — master ( 21d819...fab3d2 )
by Janusz
02:07
created

SmslabsClient::getAvailableSenders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
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
    public function __construct($appKey, $secretKey)
63
    {
64
        $this->client = new HttpClient($appKey, $secretKey);
65
    }
66
67
    /**
68
     * @param HttpClient $client
69
     */
70
    public function setClient($client)
71
    {
72
        $this->client = $client;
73
    }
74
75
    /**
76
     * @param boolean $isFlashMessage
77
     * @return SmslabsClient $this
78
     */
79
    public function setIsFlashMessage($isFlashMessage)
80
    {
81
        $this->isFlashMessage = (bool)$isFlashMessage;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $senderId
88
     * @return SmslabsClient $this
89
     */
90
    public function setSenderId($senderId)
91
    {
92
        $this->senderId = $senderId;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param int $expirationMinutes
99
     * @return SmslabsClient $this
100
     */
101
    public function setExpiration($expirationMinutes)
102
    {
103
        if ($expirationMinutes < 1 || $expirationMinutes > 5520) {
104
            throw new \InvalidArgumentException('Valid values: 1 - 5520');
105
        }
106
107
        $this->expirationMinutes = (int)$expirationMinutes;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param \DateTime $sendDateTime
114
     * @return SmslabsClient $this
115
     */
116
    public function setSendDate(\DateTime $sendDateTime)
117
    {
118
        $this->sendDateTime = $sendDateTime;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param string $phoneNumber
125
     * @param string $message
126
     * @param bool $isFlashMessage
127
     * @param int $expirationMinutes
128
     * @param \DateTime $sendDateTime
129
     * @return SmslabsClient $this
130
     */
131
    public function add(
132
        $phoneNumber,
133
        $message,
134
        $isFlashMessage = null,
135
        $expirationMinutes = null,
136
        \DateTime $sendDateTime = null
137
    ) {
138
        if ($this->checkPhoneNumber($phoneNumber) === false) {
139
            throw new \InvalidArgumentException('Invalid phone number');
140
        }
141
142
        $sms = [
143
            'phone_number' => $phoneNumber,
144
            'message' => $message,
145
            'flash' => $isFlashMessage === null ? (int)$this->isFlashMessage : (int)$isFlashMessage,
146
            'expiration' => $expirationMinutes === null ? (int)$this->expirationMinutes : (int)$expirationMinutes,
147
            'sender_id' => $this->senderId,
148
        ];
149
150
        if ($sendDateTime instanceof \DateTime) {
151
            $sms['send_date'] = (int)$sendDateTime->getTimestamp();
152
        } elseif ($this->sendDateTime instanceof \DateTime) {
153
            $sms['send_date'] = (int)$this->sendDateTime->getTimestamp();
154
        }
155
156
        $this->smsToSend[] = $sms;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param string $phoneNumber
163
     * @return bool
164
     */
165
    private function checkPhoneNumber($phoneNumber)
166
    {
167
        return (bool)preg_match('/^\+[0-9]{10,13}$/', $phoneNumber);
168
    }
169
170
    /**
171
     * @return SmslabsClient $this
172
     */
173
    public function send()
174
    {
175
        if (empty($this->smsToSend)) {
176
            throw new EmptySMSQueueException('No messages to send');
177
        }
178
179
        if ($this->senderId === null) {
180
            throw new \InvalidArgumentException('SenderId is missing');
181
        }
182
183
        foreach ($this->smsToSend as $key => $sms) {
184
            $httpResponse = $this->client->sendRequest(self::SEND_SMS_URL, $sms, 'PUT');
185
186
            $this->smsStatus[] = new SmsSentResponse($httpResponse['account'], $httpResponse['sms_id']);
187
188
            unset($this->smsToSend[$key]);
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * @return SmsSentResponse[]
196
     */
197
    public function getSentStatus()
198
    {
199
        return $this->smsStatus;
200
    }
201
202
    /**
203
     * @return Sender[]
204
     */
205
    public function getAvailableSenders()
206
    {
207
        $sendersResponse = $this->client->sendRequest(self::SENDERS_URL);
208
209
        $list = [];
210
211
        foreach ($sendersResponse as $sender) {
212
            $list[] = Sender::createFromResponseArray($sender);
213
        }
214
215
        return $list;
216
    }
217
218
    /**
219
     * @return AccountBalance
220
     */
221
    public function getAccountBalance()
222
    {
223
        $balanceResponse = $this->client->sendRequest(self::ACCOUNT_URL);
224
225
        return new AccountBalance($balanceResponse['account'] / 100);
226
    }
227
228
    /**
229
     * @return SmsIn[]
230
     */
231
    public function getSmsIn()
232
    {
233
        $smsInResponse = $this->client->sendRequest(self::SMS_IN_URL);
234
235
        $list = [];
236
237
        foreach ($smsInResponse as $smsIn) {
238
            $list[] = SmsIn::createFromResponseArray($smsIn);
239
        }
240
241
        return $list;
242
    }
243
244
    /**
245
     * @param int $offset
246
     * @param int $limit
247
     * @return SmsOut[]
248
     */
249
    public function getSmsOut($offset = 0, $limit = 100)
250
    {
251
        $smsOutResponse = $this->client->sendRequest(self::SMS_LIST_URL.'?offset='.$offset.'&limit='.$limit);
252
253
        $list = [];
254
255
        foreach ($smsOutResponse as $smsOut) {
256
            $list[] = SmsOut::createFromResponseArray($smsOut);
257
        }
258
259
        return $list;
260
    }
261
262
    /**
263
     * @param string $smsId
264
     * @return SmsDetails
265
     */
266
    public function getSmsDetails($smsId)
267
    {
268
        if ($this->senderId === null) {
269
            throw new \InvalidArgumentException('Sender ID');
270
        }
271
272
        $detailsResponse = $this->client->sendRequest(self::SMS_STATUS_URL.'?id='.$smsId);
273
274
        $smsDetails = SmsDetails::createFromResponseArray($detailsResponse);
275
276
        return $smsDetails;
277
    }
278
279
    /**
280
     * @return array[]
281
     */
282
    public function getSmsQueue()
283
    {
284
        return $this->smsToSend;
285
    }
286
}
287