Completed
Push — master ( 020c5c...9dbce5 )
by Julián
08:49
created

Result::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 15
nc 8
nop 4
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
/**
13
 * Push result abstraction.
14
 */
15
class Result implements \JsonSerializable
16
{
17
    const STATUS_SUCCESS = 'success';
18
    const STATUS_ERROR = 'error';
19
20
    /**
21
     * Device token.
22
     *
23
     * @var string
24
     */
25
    protected $token;
26
27
    /**
28
     * Result time.
29
     *
30
     * @var \DateTime
31
     */
32
    protected $date;
33
34
    /**
35
     * Result status.
36
     *
37
     * @var int
38
     */
39
    protected $status;
40
41
    /**
42
     * Result status message.
43
     *
44
     * @var string
45
     */
46
    protected $statusMessage;
47
48
    /**
49
     * @param string             $token
50
     * @param int|\DateTime|null $date
51
     * @param string             $status
52
     * @param string|null        $statusMessage
53
     *
54
     * @throws \InvalidArgumentException
55
     */
56
    public function __construct(
57
        $token,
58
        $date = null,
59
        $status = self::STATUS_SUCCESS,
60
        $statusMessage = null
61
    ) {
62
        $this->token = $token;
63
64
        if (is_int($date)) {
65
            $date = \DateTime::createFromFormat('U', (string) $date);
66
        }
67
        if ($date === null) {
68
            $date = new \DateTime('now', new \DateTimeZone('UTC'));
69
        }
70
71
        if (!$date instanceof \DateTime) {
72
            throw new \InvalidArgumentException('Wrong date value provided');
73
        }
74
        $this->setDate($date);
75
76
        $this->setStatus($status);
77
        $this->setStatusMessage($statusMessage);
78
    }
79
80
    /**
81
     * Retrieve result device token.
82
     *
83
     * @return string
84
     */
85
    public function getToken()
86
    {
87
        return $this->token;
88
    }
89
90
    /**
91
     * Set result device token.
92
     *
93
     * @param string $token
94
     *
95
     * @return $this
96
     */
97
    public function setToken($token)
98
    {
99
        $this->token = trim($token);
100
101
        return $this;
102
    }
103
104
    /**
105
     * Retrieve result time.
106
     *
107
     * @return \DateTime
108
     */
109
    public function getDate()
110
    {
111
        return $this->date;
112
    }
113
114
    /**
115
     * Set result time.
116
     *
117
     * @param \DateTime $date
118
     *
119
     * @return $this
120
     */
121
    public function setDate(\DateTime $date)
122
    {
123
        $this->date = clone $date;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Retrieve result status.
130
     *
131
     * @return int
132
     */
133
    public function getStatus()
134
    {
135
        return $this->status;
136
    }
137
138
    /**
139
     * Check successful status.
140
     *
141
     * @return bool
142
     */
143
    public function isSuccess()
144
    {
145
        return $this->status === static::STATUS_SUCCESS;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->status (integer) and static::STATUS_SUCCESS (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
146
    }
147
148
    /**
149
     * Check error status.
150
     *
151
     * @return bool
152
     */
153
    public function isError()
154
    {
155
        return $this->status === static::STATUS_ERROR;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->status (integer) and static::STATUS_ERROR (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
156
    }
157
158
    /**
159
     * Set result status.
160
     *
161
     * @param string $status
162
     *
163
     * @throws \InvalidArgumentException
164
     *
165
     * @return $this
166
     */
167
    public function setStatus($status)
168
    {
169
        if (!in_array($status, [static::STATUS_SUCCESS, static::STATUS_ERROR])) {
170
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid status', $status));
171
        }
172
173
        $this->status = $status;
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type integer, but $status is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
174
175
        return $this;
176
    }
177
178
    /**
179
     * Retrieve result status message.
180
     *
181
     * @return string
182
     */
183
    public function getStatusMessage()
184
    {
185
        return $this->statusMessage;
186
    }
187
188
    /**
189
     * Check successful status message.
190
     *
191
     * @param string|null $statusMessage
192
     *
193
     * @return $this
194
     */
195
    public function setStatusMessage($statusMessage = null)
196
    {
197
        $this->statusMessage = $statusMessage !== null ? trim($statusMessage) : null;
198
199
        return $this;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     *
205
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|integer>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
206
     */
207
    public function jsonSerialize()
208
    {
209
        return [
210
            'token' => $this->token,
211
            'date' => $this->date->format('c'),
212
            'status' => $this->status,
213
            'statusMessage' => $this->statusMessage,
214
        ];
215
    }
216
}
217