Completed
Push — master ( 8f139f...a8687a )
by Joachim
04:58
created

Details::init()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 6
Ratio 35.29 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 6
loc 17
ccs 8
cts 10
cp 0.8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 0
crap 4.128
1
<?php
2
namespace Loevgaard\Linkmobility\Response\BatchStatus;
3
4
use Loevgaard\Linkmobility\Exception\InvalidResponseException;
5
use Loevgaard\Linkmobility\Response\Response;
6
7
class Details extends Response
8
{
9
    /**
10
     * The batch is ready for processing. The batch will be processed shortly after sendtime has passed.
11
     */
12
    const STATE_QUEUED = 'QUEUED';
13
14
    /**
15
     * The batch is currently processing. Messages are being sent, and the "buffered",
16
     * "received" and "rejected" numbers will be updated as it is processed.
17
     */
18
    const STATE_RUNNING = 'RUNNING';
19
20
    /**
21
     * The batch has finished processing. Up to 48 hours after this the messages will still be sent out for delivery.
22
     */
23
    const STATE_DONE = 'DONE';
24
25
    /**
26
     * @var \DateTimeImmutable
27
     */
28
    protected $sendTime;
29
30
    /**
31
     * @var int
32
     */
33
    protected $batchId;
34
35
    /**
36
     * @var string
37
     */
38
    protected $state;
39
40
    /**
41
     * @throws InvalidResponseException
42
     */
43 1
    public function init() : void
44
    {
45 1 View Code Duplication
        if (isset($this->data->sendtime)) {
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...
46 1
            $this->sendTime = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', $this->data->sendtime);
47 1
            if ($this->sendTime === false) {
48
                throw new InvalidResponseException(
49
                    '`sendtime` does not have the correct format. Value given: '.$this->data->sendtime
50
                );
51
            }
52
        }
53
54 1
        if (isset($this->data->batchid)) {
55 1
            $this->batchId = (int)$this->data->batchid;
56
        }
57
58 1
        $this->state = $this->data->state ?? null;
59 1
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function isDone() : bool
65
    {
66
        return $this->state === static::STATE_DONE;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isQueued() : bool
73
    {
74
        return $this->state === static::STATE_QUEUED;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isRunning() : bool
81
    {
82
        return $this->state === static::STATE_RUNNING;
83
    }
84
85
    /**
86
     * @return \DateTimeImmutable
87
     */
88 1
    public function getSendTime(): \DateTimeImmutable
89
    {
90 1
        return $this->sendTime;
91
    }
92
93
    /**
94
     * @return int
95
     */
96 1
    public function getBatchId(): int
97
    {
98 1
        return $this->batchId;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 1
    public function getState(): string
105
    {
106 1
        return $this->state;
107
    }
108
}
109