1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Unified push notification services abstraction (http://github.com/juliangut/tify). |
5
|
|
|
* |
6
|
|
|
* @license BSD-3-Clause |
7
|
|
|
* @link https://github.com/juliangut/tify |
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jgut\Tify; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Push result abstraction. |
15
|
|
|
*/ |
16
|
|
|
class Result implements \JsonSerializable |
17
|
|
|
{ |
18
|
|
|
const STATUS_SUCCESS = 'success'; |
19
|
|
|
const STATUS_INVALID_DEVICE = 'invalidDevice'; |
20
|
|
|
const STATUS_INVALID_MESSAGE = 'invalidMessage'; |
21
|
|
|
const STATUS_RATE_ERROR = 'rateError'; |
22
|
|
|
const STATUS_AUTH_ERROR = 'authError'; |
23
|
|
|
const STATUS_SERVER_ERROR = 'serverError'; |
24
|
|
|
const STATUS_UNKNOWN_ERROR = 'unknownError'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Device token. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $token; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Result time. |
35
|
|
|
* |
36
|
|
|
* @var \DateTime |
37
|
|
|
*/ |
38
|
|
|
protected $date; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Result status. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $status; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Result status message. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $statusMessage; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $token |
56
|
|
|
* @param \DateTime $date |
57
|
|
|
* @param string $status |
58
|
|
|
* @param string $statusMessage |
|
|
|
|
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
$token, |
64
|
|
|
\DateTime $date, |
65
|
|
|
$status = self::STATUS_SUCCESS, |
66
|
|
|
$statusMessage = null |
67
|
|
|
) { |
68
|
|
|
$this->token = $token; |
69
|
|
|
$this->setDate($date); |
70
|
|
|
$this->setStatus($status); |
71
|
|
|
$this->setStatusMessage($statusMessage); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieve result device token. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getToken() |
80
|
|
|
{ |
81
|
|
|
return $this->token; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set result device token. |
86
|
|
|
* |
87
|
|
|
* @param string $token |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setToken($token) |
92
|
|
|
{ |
93
|
|
|
$this->token = trim($token); |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve result time. |
100
|
|
|
* |
101
|
|
|
* @return \DateTime |
102
|
|
|
*/ |
103
|
|
|
public function getDate() |
104
|
|
|
{ |
105
|
|
|
return $this->date; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set result time. |
110
|
|
|
* |
111
|
|
|
* @param \DateTime $date |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setDate(\DateTime $date) |
116
|
|
|
{ |
117
|
|
|
$this->date = clone $date; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Retrieve result status. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getStatus() |
128
|
|
|
{ |
129
|
|
|
return $this->status; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check successful status. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isSuccess() |
138
|
|
|
{ |
139
|
|
|
return $this->status === static::STATUS_SUCCESS; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check error status. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isError() |
148
|
|
|
{ |
149
|
|
|
return $this->status !== static::STATUS_SUCCESS; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set result status. |
154
|
|
|
* |
155
|
|
|
* @param string $status |
156
|
|
|
* |
157
|
|
|
* @throws \InvalidArgumentException |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function setStatus($status) |
162
|
|
|
{ |
163
|
|
|
$self = new \ReflectionClass(static::class); |
164
|
|
|
if (!in_array($status, $self->getConstants())) { |
165
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid status', $status)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->status = $status; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Retrieve result status message. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getStatusMessage() |
179
|
|
|
{ |
180
|
|
|
return $this->statusMessage; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Check successful status message. |
185
|
|
|
* |
186
|
|
|
* @param string $statusMessage |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function setStatusMessage($statusMessage = null) |
191
|
|
|
{ |
192
|
|
|
$this->statusMessage = $statusMessage !== null ? trim($statusMessage) : null; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
* |
200
|
|
|
* @return string[] |
|
|
|
|
201
|
|
|
*/ |
202
|
|
|
public function jsonSerialize() |
203
|
|
|
{ |
204
|
|
|
return [ |
205
|
|
|
'token' => $this->token, |
206
|
|
|
'date' => $this->date->format('c'), |
207
|
|
|
'status' => $this->status, |
208
|
|
|
'statusMessage' => $this->statusMessage, |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.