DeliveryReport::__construct()   C
last analyzed

Complexity

Conditions 10
Paths 133

Size

Total Lines 49
Code Lines 27

Duplication

Lines 14
Ratio 28.57 %

Code Coverage

Tests 28
CRAP Score 10

Importance

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

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 declare(strict_types=1);
2
namespace Loevgaard\Linkmobility\Webhook;
3
4
use Loevgaard\Linkmobility\Exception\InvalidDateTimeFormatException;
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 \DateTimeImmutable
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 \DateTimeImmutable
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
    /**
99
     * DeliveryReport constructor.
100
     * @param RequestInterface $request
101
     * @throws InvalidDateTimeFormatException
102
     */
103 3
    public function __construct(RequestInterface $request)
104
    {
105 3
        $this->request = $request;
106
107 3
        parse_str($this->request->getUri()->getQuery(), $query);
108
109
        // strings are just assigned if set
110 3
        $this->status = $query['status'] ?? null;
111 3
        $this->reason = $query['reason'] ?? null;
112 3
        $this->messageId = $query['msgid'] ?? null;
113 3
        $this->to = $query['to'] ?? null;
114 3
        $this->returnData = $query['returndata'] ?? null;
115
116 3 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...
117 3
            $this->receiveTime = \DateTimeImmutable::createFromFormat('U', $query['receivetime']);
118 3
            if ($this->receiveTime === false) {
119 1
                throw new InvalidDateTimeFormatException(
120 1
                    'The format of `receivetime` is wrong. Value given: '.$query['receivetime']
121
                );
122
            }
123
        }
124
125 2
        if (isset($query['statuscode'])) {
126 2
            $this->statusCode = (int)$query['statuscode'];
127
        }
128
129 2 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...
130 2
            $this->logDate = \DateTimeImmutable::createFromFormat('Y_m_d', $query['logdate']);
131 2
            if ($this->logDate === false) {
132 1
                throw new InvalidDateTimeFormatException('The format of `logdate` is wrong. Value given: '.$query['logdate']);
133
            }
134
        }
135
136 1
        if (isset($query['mcc'])) {
137 1
            $this->mcc = (int)$query['mcc'];
138
        }
139
140 1
        if (isset($query['mnc'])) {
141 1
            $this->mnc = (int)$query['mnc'];
142
        }
143
144 1
        if (isset($query['batchid'])) {
145 1
            $this->batchId = (int)$query['batchid'];
146
        }
147
148 1
        if (isset($query['push_price'])) {
149 1
            $this->pushPrice = (int)$query['push_price'];
150
        }
151 1
    }
152
153
    /**
154
     * Returns true if the respective message was received by the recipient
155
     *
156
     * @return bool
157
     */
158 1
    public function isReceived() : bool
159
    {
160 1
        return $this->status === static::STATUS_RECEIVED;
161
    }
162
163
    /**
164
     * Returns true if the respective message was rejected
165
     *
166
     * @return bool
167
     */
168 1
    public function isRejected() : bool
169
    {
170 1
        return $this->status === static::STATUS_REJECTED;
171
    }
172
173
    /**
174
     * Returns true if the respective message is buffered
175
     *
176
     * @return bool
177
     */
178 1
    public function isBuffered() : bool
179
    {
180 1
        return $this->status === static::STATUS_BUFFERED;
181
    }
182
183
    /**
184
     * Returns true if the respective message has expired
185
     *
186
     * @return bool
187
     */
188 1
    public function isExpired() : bool
189
    {
190 1
        return $this->status === static::STATUS_EXPIRED;
191
    }
192
193
    /**
194
     * @return RequestInterface
195
     */
196 1
    public function getRequest(): RequestInterface
197
    {
198 1
        return $this->request;
199
    }
200
201
    /**
202
     * @return string
203
     */
204 1
    public function getStatus(): string
205
    {
206 1
        return $this->status;
207
    }
208
209
    /**
210
     * @return string
211
     */
212 1
    public function getReason(): string
213
    {
214 1
        return $this->reason;
215
    }
216
217
    /**
218
     * @return \DateTimeImmutable
219
     */
220 1
    public function getReceiveTime(): \DateTimeImmutable
221
    {
222 1
        return $this->receiveTime;
223
    }
224
225
    /**
226
     * @return string
227
     */
228 1
    public function getMessageId(): string
229
    {
230 1
        return $this->messageId;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 1
    public function getTo(): string
237
    {
238 1
        return $this->to;
239
    }
240
241
    /**
242
     * @return int
243
     */
244 1
    public function getStatusCode(): int
245
    {
246 1
        return $this->statusCode;
247
    }
248
249
    /**
250
     * @return string
251
     */
252 1
    public function getReturnData(): string
253
    {
254 1
        return $this->returnData;
255
    }
256
257
    /**
258
     * @return \DateTimeImmutable
259
     */
260 1
    public function getLogDate(): \DateTimeImmutable
261
    {
262 1
        return $this->logDate;
263
    }
264
265
    /**
266
     * @return int
267
     */
268 1
    public function getMcc(): int
269
    {
270 1
        return $this->mcc;
271
    }
272
273
    /**
274
     * @return int
275
     */
276 1
    public function getMnc(): int
277
    {
278 1
        return $this->mnc;
279
    }
280
281
    /**
282
     * @return int
283
     */
284 1
    public function getBatchId(): int
285
    {
286 1
        return $this->batchId;
287
    }
288
289
    /**
290
     * @return int
291
     */
292 1
    public function getPushPrice(): int
293
    {
294 1
        return $this->pushPrice;
295
    }
296
}
297