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

AbstractNetgsmMessage::validateRecipients()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 0
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
     * @param  string|array|$recipients
125
     * @return $this
126
     */
127
    public function setRecipients($recipients)
128
    {
129
        if (! is_array($recipients)) {
130
            $this->recipients = explode(',', $recipients);
131
        } else {
132
            $this->recipients = $recipients;
133
        }
134
135
        return $this;
136
    }
137
138
    /**
139
     * @return string[]
140
     */
141
    public function getRecipients(): array
142
    {
143
        return $this->recipients;
144
    }
145
146
    /**
147
     * @param  null  $header
148
     * @return AbstractNetgsmMessage
149
     */
150
    public function setHeader($header): self
151
    {
152
        $this->header = $header;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getHeader(): string
161
    {
162
        return $this->header ?? $this->defaults['header'];
163
    }
164
165
    /**
166
     * @param  string  $message
167
     * @return AbstractNetgsmMessage
168
     */
169
    public function setMessage(string $message): self
170
    {
171
        $this->message = $message;
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getMessage(): string
180
    {
181
        return $this->message;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getSendMethod(): string
188
    {
189
        return $this->sendMethod ?? $this->defaults['sms_sending_method'];
190
    }
191
192
    /**
193
     * @param  string  $sendMethod
194
     * @return $this
195
     * @throws Exception
196
     */
197
    public function setSendMethod(string $sendMethod): self
198
    {
199
        if (! in_array($sendMethod, $this->sendMethods)) {
200
            throw new Exception($sendMethod.' method is not allowed');
201
        }
202
203
        $this->sendMethod = $sendMethod;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return bool
210
     */
211
    public function isAuthorizedData(): bool
212
    {
213
        return $this->authorizedData;
214
    }
215
216
    /**
217
     * @param  bool  $authorizedData
218
     * @return AbstractNetgsmMessage
219
     */
220
    public function setAuthorizedData(bool $authorizedData): self
221
    {
222
        $this->authorizedData = $authorizedData;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getUrl(): string
231
    {
232
        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...
233
    }
234
235
    /**
236
     * @throws IncorrectPhoneNumberFormatException
237
     */
238
    protected function validateRecipients(): void
239
    {
240
        if (count($this->recipients) == 0) {
241
            throw new IncorrectPhoneNumberFormatException();
242
        }
243
        foreach ($this->recipients as $recipient) {
244
            if (strstr($recipient, ' ') || strlen($recipient) < 10) {
245
                throw new IncorrectPhoneNumberFormatException();
246
            }
247
        }
248
    }
249
250
    /**
251
     * @return string
252
     */
253
    public function body(): array
254
    {
255
        return array_merge(array_flip($this->fields), array_filter($this->mappers()));
256
    }
257
258
    /**
259
     * @param  array  $defaults
260
     * @return AbstractNetgsmMessage
261
     */
262
    public function setDefaults(array $defaults): self
263
    {
264
        $this->defaults = $defaults;
265
266
        return $this;
267
    }
268
269
    /**
270
     * @param  Carbon  $startDate
271
     * @return AbstractNetgsmMessage
272
     */
273
    public function setStartDate(Carbon $startDate): self
274
    {
275
        $this->startDate = $startDate;
276
277
        return $this;
278
    }
279
280
    /**
281
     * @param  Carbon  $endDate
282
     * @return AbstractNetgsmMessage
283
     */
284
    public function setEndDate(Carbon $endDate): self
285
    {
286
        $this->endDate = $endDate;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return mixed
293
     */
294
    public function getJobId(): string
295
    {
296
        return $this->jobId;
297
    }
298
299
    /**
300
     * @return $this
301
     * @throws CouldNotSendNotification
302
     */
303
    public function parseResponse(): self
304
    {
305
        $result = explode(' ', $this->response);
306
307
        if (! isset($result[0])) {
308
            throw new CouldNotSendNotification(NetgsmErrors::NETGSM_GENERAL_ERROR);
309
        }
310
311
        if (! in_array($result[0], self::SUCCESS_CODES)) {
312
            $message = $this->errorCodes[$result[0]];
313
            throw new CouldNotSendNotification($message);
314
        }
315
316
        $this->code = $result[0];
317
        $this->jobId = $result[1];
318
319
        return $this;
320
    }
321
322
    /**
323
     * @return $this
324
     * @throws CouldNotSendNotification
325
     * @throws GuzzleException
326
     */
327
    protected function sendViaGet(): self
328
    {
329
        $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...
330
331
        return $this->parseResponse();
332
    }
333
334
    /**
335
     * @return $this
336
     * @throws CouldNotSendNotification
337
     * @throws GuzzleException
338
     */
339
    protected function sendViaXml(): self
340
    {
341
        $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...
342
            'Content-Type' => 'text/xml; charset=UTF8',
343
        ]);
344
345
        return $this->parseResponse();
346
    }
347
348
    /**
349
     * @return $this
350
     * @throws IncorrectPhoneNumberFormatException
351
     */
352
    public function send()
353
    {
354
        $this->validateRecipients();
355
        $method = 'sendVia'.$this->getSendMethod();
356
357
        return call_user_func([$this, $method]);
358
    }
359
}
360