Completed
Pull Request — master (#7)
by
unknown
01:23
created

AbstractNetgsmMessage   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 369
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 3
dl 0
loc 369
rs 9.92
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 5 1
mappers() 0 1 ?
createXmlPost() 0 1 ?
A setRecipients() 0 10 2
A getRecipients() 0 4 1
A setHeader() 0 6 1
A getHeader() 0 4 1
A setMessage() 0 6 1
A getMessage() 0 4 1
A getSendMethod() 0 4 1
A setSendMethod() 0 10 2
A isAuthorizedData() 0 4 1
A setAuthorizedData() 0 6 1
A getUrl() 0 4 1
A validateRecipients() 0 11 5
A body() 0 4 1
A setDefaults() 0 6 1
A setStartDate() 0 6 1
A setEndDate() 0 6 1
A getJobId() 0 4 1
A parseResponse() 0 18 3
A sendViaGet() 0 6 1
A sendViaXml() 0 8 1
A send() 0 7 1
1
<?php
2
3
namespace TarfinLabs\Netgsm\Sms;
4
5
use Exception;
6
use GuzzleHttp\Exception\GuzzleException;
7
use Illuminate\Support\Carbon;
8
use Psr\Http\Message\ResponseInterface;
9
use TarfinLabs\Netgsm\Exceptions\CouldNotSendNotification;
10
use TarfinLabs\Netgsm\Exceptions\IncorrectPhoneNumberFormatException;
11
use TarfinLabs\Netgsm\NetgsmApiClient;
12
use TarfinLabs\Netgsm\NetgsmErrors;
13
14
abstract class AbstractNetgsmMessage extends NetgsmApiClient
15
{
16
    private const SUCCESS_CODES = [
17
        '00', '01', '02',
18
    ];
19
20
    protected $sendMethods = ['xml', 'get'];
21
22
    /**
23
     * @var string
24
     */
25
    protected $sendMethod;
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $recipients = [];
31
32
    /**
33
     * @var null
34
     */
35
    protected $header = null;
36
37
    /**
38
     * @var Carbon
39
     */
40
    protected $startDate;
41
42
    /**
43
     * @var Carbon
44
     */
45
    protected $endDate;
46
47
    /**
48
     * @var string
49
     */
50
    protected $code;
51
52
    /**
53
     * @var string
54
     */
55
    protected $jobId;
56
57
    /**
58
     * @var array
59
     */
60
    protected $defaults = [];
61
62
    /**
63
     * @var string message
64
     */
65
    protected $message;
66
67
    /**
68
     * authorized data parameter.
69
     *
70
     * @see https://www.netgsm.com.tr/dokuman/#http-get-sms-g%C3%B6nderme
71
     * @see https://www.netgsm.com.tr/dokuman/#xml-post-sms-g%C3%B6nderme
72
     *
73
     * @var bool
74
     */
75
    protected $authorizedData = false;
76
77
    /**
78
     * @var ResponseInterface
79
     */
80
    protected $response;
81
82
    /**
83
     * @var array
84
     */
85
    protected $fields = [];
86
87
    /**
88
     * @var array
89
     */
90
    protected $errorCodes;
91
92
    /**
93
     * @param  string  $message
94
     * @param  array  $defaults
95
     * @return static
96
     */
97
    public static function create(string $message = null, array $defaults = [])
98
    {
99
        return new static($message, $defaults);
100
    }
101
102
    /**
103
     * AbstractNetgsmMessage constructor.
104
     * @param  array  $defaults
105
     * @param  string  $message
106
     */
107
    public function __construct(string $message = null, array $defaults = [])
108
    {
109
        $this->defaults = $defaults;
110
        $this->message = $message;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    abstract protected function mappers(): array;
117
118
    /**
119
     * @return string
120
     */
121
    abstract protected function createXmlPost(): string;
122
123
    /**
124
     * set's the sms recipients
125
     * it can be array or string
126
     *
127
     * @param  string|array|$recipients
128
     * @return $this
129
     */
130
    public function setRecipients($recipients)
131
    {
132
        if (!is_array($recipients)) {
133
            $this->recipients = explode(',', $recipients);
134
        } else {
135
            $this->recipients = $recipients;
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return string[]
143
     */
144
    public function getRecipients(): array
145
    {
146
        return $this->recipients;
147
    }
148
149
    /**
150
     * set's the sms origin
151
     * @see https://www.netgsm.com.tr/dokuman/#g%C3%B6nderici-ad%C4%B1-sorgulama
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
     * set's the message body
173
     *
174
     * @param  string  $message
175
     * @return AbstractNetgsmMessage
176
     */
177
    public function setMessage(string $message): self
178
    {
179
        $this->message = $message;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getMessage(): string
188
    {
189
        return $this->message;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getSendMethod(): string
196
    {
197
        return $this->sendMethod ?? $this->defaults['sms_sending_method'];
198
    }
199
200
    /**
201
     * set's the sms sending method
202
     * allowed send methods are (xml, get)
203
     *
204
     * @param  string  $sendMethod
205
     * @return $this
206
     * @throws Exception
207
     */
208
    public function setSendMethod(string $sendMethod): self
209
    {
210
        if (! in_array($sendMethod, $this->sendMethods)) {
211
            throw new Exception($sendMethod.' method is not allowed');
212
        }
213
214
        $this->sendMethod = $sendMethod;
215
216
        return $this;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function isAuthorizedData(): bool
223
    {
224
        return $this->authorizedData;
225
    }
226
227
    /**
228
     * @param  bool  $authorizedData
229
     * @return AbstractNetgsmMessage
230
     */
231
    public function setAuthorizedData(bool $authorizedData): self
232
    {
233
        $this->authorizedData = $authorizedData;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getUrl(): string
242
    {
243
        return $this->url.'/'.$this->getSendMethod();
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
244
    }
245
246
    /**
247
     * validates the sms recipients
248
     *
249
     * @throws IncorrectPhoneNumberFormatException
250
     */
251
    protected function validateRecipients(): void
252
    {
253
        if (count($this->recipients) == 0) {
254
            throw new IncorrectPhoneNumberFormatException();
255
        }
256
        foreach ($this->recipients as $recipient) {
257
            if (strstr($recipient, ' ') || strlen($recipient) < 10) {
258
                throw new IncorrectPhoneNumberFormatException();
259
            }
260
        }
261
    }
262
263
    /**
264
     * generates the request body for append sms sending endpoint
265
     *
266
     * @return string
267
     */
268
    public function body(): array
269
    {
270
        return array_merge(array_flip($this->fields), array_filter($this->mappers()));
271
    }
272
273
    /**
274
     * @param  array  $defaults
275
     * @return AbstractNetgsmMessage
276
     */
277
    public function setDefaults(array $defaults): self
278
    {
279
        $this->defaults = $defaults;
280
281
        return $this;
282
    }
283
284
    /**
285
     * @param  Carbon  $startDate
286
     * @return AbstractNetgsmMessage
287
     */
288
    public function setStartDate(Carbon $startDate): self
289
    {
290
        $this->startDate = $startDate;
291
292
        return $this;
293
    }
294
295
    /**
296
     * @param  Carbon  $endDate
297
     * @return AbstractNetgsmMessage
298
     */
299
    public function setEndDate(Carbon $endDate): self
300
    {
301
        $this->endDate = $endDate;
302
303
        return $this;
304
    }
305
306
    /**
307
     * @return mixed
308
     */
309
    public function getJobId(): string
310
    {
311
        return $this->jobId;
312
    }
313
314
    /**
315
     * parses the response from api and returns job id.
316
     *
317
     * @return $this
318
     * @throws CouldNotSendNotification
319
     */
320
    public function parseResponse(): self
321
    {
322
        $result = explode(' ', $this->response);
323
324
        if (! isset($result[0])) {
325
            throw new CouldNotSendNotification(NetgsmErrors::NETGSM_GENERAL_ERROR);
326
        }
327
328
        if (!in_array($result[0], self::SUCCESS_CODES)) {
329
            $message = $this->errorCodes[$result[0]];
330
            throw new CouldNotSendNotification($message);
331
        }
332
333
        $this->code = $result[0];
334
        $this->jobId = $result[1];
335
336
        return $this;
337
    }
338
339
    /**
340
     * sends a sms via get method
341
     *
342
     * @return $this
343
     * @throws CouldNotSendNotification
344
     * @throws GuzzleException
345
     */
346
    protected function sendViaGet(): self
347
    {
348
        $this->response = $this->callApi('GET', $this->getUrl(), $this->body());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->callApi('GET', $t...etUrl(), $this->body()) of type string is incompatible with the declared type object<Psr\Http\Message\ResponseInterface> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
349
350
        return $this->parseResponse();
351
    }
352
353
    /**
354
     * sends a sms via xml method
355
     *
356
     * @return $this
357
     * @throws CouldNotSendNotification
358
     * @throws GuzzleException
359
     */
360
    protected function sendViaXml(): self
361
    {
362
        $this->response = $this->callApi('POST', $this->getUrl(), $this->createXmlPost(), [
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->callApi('POST', $...xt/xml; charset=UTF8')) of type string is incompatible with the declared type object<Psr\Http\Message\ResponseInterface> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
363
            'Content-Type' => 'text/xml; charset=UTF8',
364
        ]);
365
366
        return $this->parseResponse();
367
    }
368
369
    /**
370
     * sends a sms via specified sending method
371
     *
372
     * @return $this
373
     * @throws IncorrectPhoneNumberFormatException
374
     */
375
    public function send()
376
    {
377
        $this->validateRecipients();
378
        $method = 'sendVia'.$this->getSendMethod();
379
380
        return call_user_func([$this, $method]);
381
    }
382
}
383