Completed
Push — master ( c63a5d...4cdc8a )
by Joachim
13:05
created

DeliveryReport::__construct()   C

Complexity

Conditions 10
Paths 133

Size

Total Lines 49
Code Lines 27

Duplication

Lines 14
Ratio 28.57 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 133
nop 1
dl 14
loc 49
rs 5.2413
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Loevgaard\Linkmobility\Webhook;
3
4
use Loevgaard\Linkmobility\Exception\InvalidWebhookException;
5
use Psr\Http\Message\RequestInterface;
6
7
class DeliveryReport
8
{
9
    /**
10
     * The message is received
11
     */
12
    const STATUS_RECEIVED = 'received';
13
14
    /**
15
     * The message is rejected by the SMSC. (Please see the list of status codes for an explanation)
16
     */
17
    const STATUS_REJECTED = 'rejected';
18
19
    /**
20
     * The message was not delivered and will try to get sent again at another time
21
     */
22
    const STATUS_BUFFERED = 'buffered';
23
24
    /**
25
     * The validity period has expired, the message wasn't delivered
26
     */
27
    const STATUS_EXPIRED = 'expired';
28
29
    /**
30
     * The HTTP request made by Linkmobility
31
     *
32
     * @var RequestInterface
33
     */
34
    protected $request;
35
36
    /**
37
     * @var string
38
     */
39
    protected $status;
40
41
    /**
42
     * @var string
43
     */
44
    protected $reason;
45
46
    /**
47
     * @var \DateTimeInterface
48
     */
49
    protected $receiveTime;
50
51
    /**
52
     * @var string
53
     */
54
    protected $messageId;
55
56
    /**
57
     * @var string
58
     */
59
    protected $to;
60
61
    /**
62
     * @see https://linkmobility.atlassian.net/wiki/spaces/COOL/pages/26017824/Delivery+report
63
     *
64
     * @var int
65
     */
66
    protected $statusCode;
67
68
    /**
69
     * @var string
70
     */
71
    protected $returnData;
72
73
    /**
74
     * @var \DateTimeInterface
75
     */
76
    protected $logDate;
77
78
    /**
79
     * @var int
80
     */
81
    protected $mcc;
82
83
    /**
84
     * @var int
85
     */
86
    protected $mnc;
87
88
    /**
89
     * @var int
90
     */
91
    protected $batchId;
92
93
    /**
94
     * @var int
95
     */
96
    protected $pushPrice;
97
98
    public function __construct(RequestInterface $request)
99
    {
100
        $this->request = $request;
101
102
        parse_str($this->request->getUri()->getQuery(), $query);
103
104
        // strings are just assigned if set
105
        $this->status = $query['status'] ?? null;
106
        $this->reason = $query['reason'] ?? null;
107
        $this->messageId = $query['msgid'] ?? null;
108
        $this->to = $query['to'] ?? null;
109
        $this->returnData = $query['returndata'] ?? null;
110
111 View Code Duplication
        if (isset($query['receivetime'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
            $this->receiveTime = \DateTimeImmutable::createFromFormat('U', $query['receivetime']);
113
            if ($this->receiveTime === false) {
114
                throw new InvalidWebhookException(
115
                    'The format of `receivetime` is wrong. Value given: '.$query['receivetime']
116
                );
117
            }
118
        }
119
120
        if (isset($query['statuscode'])) {
121
            $this->statusCode = (int)$query['statuscode'];
122
        }
123
124 View Code Duplication
        if (isset($query['logdate'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $this->logDate = \DateTimeImmutable::createFromFormat('Y_m_d', $query['logdate']);
126
            if ($this->logDate === false) {
127
                throw new InvalidWebhookException('The format of `logdate` is wrong. Value given: '.$query['logdate']);
128
            }
129
        }
130
131
        if (isset($query['mcc'])) {
132
            $this->mcc = (int)$query['mcc'];
133
        }
134
135
        if (isset($query['mnc'])) {
136
            $this->mnc = (int)$query['mnc'];
137
        }
138
139
        if (isset($query['batchid'])) {
140
            $this->batchId = (int)$query['batchid'];
141
        }
142
143
        if (isset($query['push_price'])) {
144
            $this->pushPrice = (int)$query['push_price'];
145
        }
146
    }
147
148
    /**
149
     * Returns true if the respective message was received by the recipient
150
     *
151
     * @return bool
152
     */
153
    public function isReceived() : bool
154
    {
155
        return $this->status === static::STATUS_RECEIVED;
156
    }
157
158
    /**
159
     * Returns true if the respective message was rejected
160
     *
161
     * @return bool
162
     */
163
    public function isRejected() : bool
164
    {
165
        return $this->status === static::STATUS_REJECTED;
166
    }
167
168
    /**
169
     * Returns true if the respective message is buffered
170
     *
171
     * @return bool
172
     */
173
    public function isBuffered() : bool
174
    {
175
        return $this->status === static::STATUS_BUFFERED;
176
    }
177
178
    /**
179
     * Returns true if the respective message has expired
180
     *
181
     * @return bool
182
     */
183
    public function isExpired() : bool
184
    {
185
        return $this->status === static::STATUS_EXPIRED;
186
    }
187
188
    /**
189
     * @return RequestInterface
190
     */
191
    public function getRequest(): RequestInterface
192
    {
193
        return $this->request;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getStatus(): string
200
    {
201
        return $this->status;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getReason(): string
208
    {
209
        return $this->reason;
210
    }
211
212
    /**
213
     * @return \DateTimeInterface
214
     */
215
    public function getReceiveTime(): \DateTimeInterface
216
    {
217
        return $this->receiveTime;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function getMessageId(): string
224
    {
225
        return $this->messageId;
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getTo(): string
232
    {
233
        return $this->to;
234
    }
235
236
    /**
237
     * @return int
238
     */
239
    public function getStatusCode(): int
240
    {
241
        return $this->statusCode;
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getReturnData(): string
248
    {
249
        return $this->returnData;
250
    }
251
252
    /**
253
     * @return \DateTimeInterface
254
     */
255
    public function getLogDate(): \DateTimeInterface
256
    {
257
        return $this->logDate;
258
    }
259
260
    /**
261
     * @return int
262
     */
263
    public function getMcc(): int
264
    {
265
        return $this->mcc;
266
    }
267
268
    /**
269
     * @return int
270
     */
271
    public function getMnc(): int
272
    {
273
        return $this->mnc;
274
    }
275
276
    /**
277
     * @return int
278
     */
279
    public function getBatchId(): int
280
    {
281
        return $this->batchId;
282
    }
283
284
    /**
285
     * @return int
286
     */
287
    public function getPushPrice(): int
288
    {
289
        return $this->pushPrice;
290
    }
291
}
292