Completed
Push — master ( d595bc...3de291 )
by
unknown
01:27 queued 10s
created

AbstractNetgsmMessage::setHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\Netgsm;
4
5
use Exception;
6
use GuzzleHttp\ClientInterface;
7
use Illuminate\Support\Carbon;
8
use Psr\Http\Message\ResponseInterface;
9
use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification;
10
use TarfinLabs\Netgsm\Exceptions\IncorrectPhoneNumberFormatException;
11
12
abstract class AbstractNetgsmMessage
13
{
14
    private const SUCCESS_CODES = [
15
        '00', '01', '02',
16
    ];
17
18
    protected $sendMethods = ['xml', 'get'];
19
20
    /**
21
     * @var string
22
     */
23
    protected $sendMethod;
24
25
    /**
26
     * @var string[]
27
     */
28
    protected $recipients = [];
29
30
    /**
31
     * @var null
32
     */
33
    protected $header = null;
34
35
    /**
36
     * @var Carbon
37
     */
38
    protected $startDate;
39
40
    /**
41
     * @var Carbon
42
     */
43
    protected $endDate;
44
45
    /**
46
     * @var string
47
     */
48
    protected $code;
49
50
    /**
51
     * @var string
52
     */
53
    protected $jobId;
54
55
    /**
56
     * @var array
57
     */
58
    protected $defaults = [];
59
    /**
60
     * @var array
61
     */
62
    protected $credentials = [];
63
    /**
64
     * @var ClientInterface
65
     */
66
    protected $client;
67
    /**
68
     * @var string endpoint url
69
     */
70
    protected $url;
71
    /**
72
     * @var string message
73
     */
74
    protected $message;
75
76
    /**
77
     * authorized data parameter
78
     *
79
     * @see https://www.netgsm.com.tr/dokuman/#http-get-sms-g%C3%B6nderme
80
     * @see https://www.netgsm.com.tr/dokuman/#xml-post-sms-g%C3%B6nderme
81
     *
82
     * @var bool
83
     */
84
    protected $authorizedData = false;
85
86
    /**
87
     * @var ResponseInterface
88
     */
89
    protected $response;
90
91
    /**
92
     * @var array
93
     */
94
    protected $fields = [];
95
96
    /**
97
     * @var array
98
     */
99
    protected $errorCodes;
100
101
    /**
102
     * @param  string  $message
103
     * @param  array  $defaults
104
     * @return static
105
     */
106
    public static function create(string $message = null, array $defaults = [])
107
    {
108
        return new static($message, $defaults);
109
    }
110
111
    /**
112
     * AbstractNetgsmMessage constructor.
113
     * @param  array  $defaults
114
     * @param  string  $message
115
     */
116
    public function __construct(string $message = null, array $defaults = [])
117
    {
118
        $this->defaults = $defaults;
119
        $this->message = $message;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    abstract protected function mappers(): array;
126
127
    abstract protected function createXmlPost(): string;
128
129
    /**
130
     * @param  string|array|$recipients
131
     * @return $this
132
     */
133
    public function setRecipients($recipients)
134
    {
135
        if (!is_array($recipients)) {
136
            $this->recipients = explode(',', $recipients);
137
        } else {
138
            $this->recipients = $recipients;
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * @return string[]
146
     */
147
    public function getRecipients(): array
148
    {
149
        return $this->recipients;
150
    }
151
152
    /**
153
     * @param  null  $header
154
     * @return AbstractNetgsmMessage
155
     */
156
    public function setHeader($header): self
157
    {
158
        $this->header = $header;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getHeader(): string
167
    {
168
        return $this->header ?? $this->defaults['header'];
169
    }
170
171
    /**
172
     * @param  string  $message
173
     * @return AbstractNetgsmMessage
174
     */
175
    public function setMessage(string $message): self
176
    {
177
        $this->message = $message;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getMessage(): string
186
    {
187
        return $this->message;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getSendMethod(): string
194
    {
195
        return $this->sendMethod ?? $this->defaults['sms_sending_method'];
196
    }
197
198
    /**
199
     * @param  string  $sendMethod
200
     * @return $this
201
     * @throws Exception
202
     */
203
    public function setSendMethod(string $sendMethod): self
204
    {
205
        if (!in_array($sendMethod, $this->sendMethods)) {
206
            throw new Exception($sendMethod." method is not allowed");
207
        }
208
209
        $this->sendMethod = $sendMethod;
210
        return $this;
211
    }
212
213
    /**
214
     * @return bool
215
     */
216
    public function isAuthorizedData(): bool
217
    {
218
        return $this->authorizedData;
219
    }
220
221
    /**
222
     *
223
     * @param  bool  $authorizedData
224
     * @return AbstractNetgsmMessage
225
     */
226
    public function setAuthorizedData(bool $authorizedData): AbstractNetgsmMessage
227
    {
228
        $this->authorizedData = $authorizedData;
229
        return $this;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getUrl(): string
236
    {
237
        return $this->url."/".$this->getSendMethod();
238
    }
239
240
    /**
241
     * @throws IncorrectPhoneNumberFormatException
242
     */
243
    protected function validateRecipients(): void
244
    {
245
        if (count($this->recipients) == 0) {
246
            throw new IncorrectPhoneNumberFormatException();
247
        }
248
        foreach ($this->recipients as $recipient) {
249
            if (strstr($recipient, ' ') || strlen($recipient) < 10) {
250
                throw new IncorrectPhoneNumberFormatException();
251
            }
252
        }
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function body(): array
259
    {
260
        return array_merge(array_flip($this->fields), array_filter($this->mappers()));
261
    }
262
263
    /**
264
     * @param  mixed  $client
265
     * @return AbstractNetgsmMessage
266
     */
267
    public function setClient(ClientInterface $client): self
268
    {
269
        $this->client = $client;
270
271
        return $this;
272
    }
273
274
    /**
275
     * @param  array  $defaults
276
     * @return AbstractNetgsmMessage
277
     */
278
    public function setDefaults(array $defaults): self
279
    {
280
        $this->defaults = $defaults;
281
282
        return $this;
283
    }
284
285
    /**
286
     * @param  array  $credentials
287
     * @return AbstractNetgsmMessage
288
     */
289
    public function setCredentials(array $credentials): self
290
    {
291
        $this->credentials = $credentials;
292
293
        return $this;
294
    }
295
296
    /**
297
     * @param  Carbon  $startDate
298
     * @return AbstractNetgsmMessage
299
     */
300
    public function setStartDate(Carbon $startDate): self
301
    {
302
        $this->startDate = $startDate;
303
        return $this;
304
    }
305
306
    /**
307
     * @param  Carbon  $endDate
308
     * @return AbstractNetgsmMessage
309
     */
310
    public function setEndDate(Carbon $endDate): self
311
    {
312
        $this->endDate = $endDate;
313
        return $this;
314
    }
315
316
    /**
317
     * @return string
318
     */
319
    protected function getResponseContent(): string
320
    {
321
        return $this->response->getBody()->getContents();
322
    }
323
324
    /**
325
     * @return mixed
326
     */
327
    public function getJobId(): string
328
    {
329
        return $this->jobId;
330
    }
331
332
    /**
333
     * @return $this
334
     * @throws CouldNotSendNotification
335
     */
336
    public function parseResponse(): self
337
    {
338
        $result = explode(' ', $this->getResponseContent());
339
340
        if (!isset($result[0])) {
341
            throw new CouldNotSendNotification(CouldNotSendNotification::NETGSM_GENERAL_ERROR);
342
        }
343
344
        if (!in_array($result[0], self::SUCCESS_CODES)) {
345
            $message = $this->errorCodes[$result[0]];
346
            throw new CouldNotSendNotification($message);
347
        }
348
349
        $this->code = $result[0];
350
        $this->jobId = $result[1];
351
352
        return $this;
353
    }
354
355
    /**
356
     * @return $this
357
     * @throws CouldNotSendNotification
358
     * @throws IncorrectPhoneNumberFormatException
359
     * @throws \GuzzleHttp\Exception\GuzzleException
360
     */
361
    protected function sendViaGet(): self
362
    {
363
        $query = http_build_query($this->body());
364
365
        $this->response = $this->client->request('GET', $this->getUrl().'?'.$query);
366
367
        return $this->parseResponse();
368
    }
369
370
    /**
371
     * @return $this
372
     * @throws CouldNotSendNotification
373
     * @throws \GuzzleHttp\Exception\GuzzleException
374
     */
375
    protected function sendViaXml(): self
376
    {
377
        $options = [
378
            'headers' => [
379
                'Content-Type' => 'text/xml; charset=UTF8',
380
            ],
381
            'body'    => $this->createXmlPost(),
382
        ];
383
384
        $this->response = $this->client->request('POST', $this->getUrl(), $options);
385
386
        return $this->parseResponse();
387
    }
388
389
    /**
390
     * @return $this
391
     * @throws CouldNotSendNotification
392
     * @throws IncorrectPhoneNumberFormatException
393
     * @throws \GuzzleHttp\Exception\GuzzleException
394
     */
395
    public function send()
396
    {
397
        $this->validateRecipients();
398
        $method = 'sendVia'.$this->getSendMethod();
399
400
        return call_user_func([$this, $method]);
401
    }
402
}
403