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_INVALID_DEVICE = 'invalidDevice'; |
19
|
|
|
const STATUS_INVALID_MESSAGE = 'invalidMessage'; |
20
|
|
|
const STATUS_RATE_ERROR = 'rateError'; |
21
|
|
|
const STATUS_AUTH_ERROR = 'authError'; |
22
|
|
|
const STATUS_SERVER_ERROR = 'serverError'; |
23
|
|
|
const STATUS_UNKNOWN_ERROR = 'unknownError'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Device token. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $token; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Result time. |
34
|
|
|
* |
35
|
|
|
* @var \DateTime |
36
|
|
|
*/ |
37
|
|
|
protected $date; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Result status. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $status; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Result status message. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $statusMessage; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $token |
55
|
|
|
* @param int|\DateTime|null $date |
56
|
|
|
* @param string $status |
57
|
|
|
* @param string|null $statusMessage |
58
|
|
|
* |
59
|
|
|
* @throws \InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
$token, |
63
|
|
|
$date = null, |
64
|
|
|
$status = self::STATUS_SUCCESS, |
65
|
|
|
$statusMessage = null |
66
|
|
|
) { |
67
|
|
|
$this->token = $token; |
68
|
|
|
|
69
|
|
|
if (is_int($date)) { |
70
|
|
|
$date = \DateTime::createFromFormat('U', (string) $date); |
71
|
|
|
} |
72
|
|
|
if ($date === null) { |
73
|
|
|
$date = new \DateTime('now', new \DateTimeZone('UTC')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$date instanceof \DateTime) { |
77
|
|
|
throw new \InvalidArgumentException('Wrong date value provided'); |
78
|
|
|
} |
79
|
|
|
$this->setDate($date); |
80
|
|
|
|
81
|
|
|
$this->setStatus($status); |
82
|
|
|
$this->setStatusMessage($statusMessage); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retrieve result device token. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getToken() |
91
|
|
|
{ |
92
|
|
|
return $this->token; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set result device token. |
97
|
|
|
* |
98
|
|
|
* @param string $token |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setToken($token) |
103
|
|
|
{ |
104
|
|
|
$this->token = trim($token); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve result time. |
111
|
|
|
* |
112
|
|
|
* @return \DateTime |
113
|
|
|
*/ |
114
|
|
|
public function getDate() |
115
|
|
|
{ |
116
|
|
|
return $this->date; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set result time. |
121
|
|
|
* |
122
|
|
|
* @param \DateTime $date |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setDate(\DateTime $date) |
127
|
|
|
{ |
128
|
|
|
$this->date = clone $date; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Retrieve result status. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getStatus() |
139
|
|
|
{ |
140
|
|
|
return $this->status; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check successful status. |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function isSuccess() |
149
|
|
|
{ |
150
|
|
|
return $this->status === static::STATUS_SUCCESS; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check error status. |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function isError() |
159
|
|
|
{ |
160
|
|
|
return $this->status !== static::STATUS_SUCCESS; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set result status. |
165
|
|
|
* |
166
|
|
|
* @param string $status |
167
|
|
|
* |
168
|
|
|
* @throws \InvalidArgumentException |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
public function setStatus($status) |
173
|
|
|
{ |
174
|
|
|
$ref = new \ReflectionClass(self::class); |
175
|
|
|
if (!in_array($status, $ref->getConstants())) { |
176
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid status', $status)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->status = $status; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Retrieve result status message. |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getStatusMessage() |
190
|
|
|
{ |
191
|
|
|
return $this->statusMessage; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check successful status message. |
196
|
|
|
* |
197
|
|
|
* @param string|null $statusMessage |
198
|
|
|
* |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function setStatusMessage($statusMessage = null) |
202
|
|
|
{ |
203
|
|
|
$this->statusMessage = $statusMessage !== null ? trim($statusMessage) : null; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
* |
211
|
|
|
* @return string[] |
|
|
|
|
212
|
|
|
*/ |
213
|
|
|
public function jsonSerialize() |
214
|
|
|
{ |
215
|
|
|
return [ |
216
|
|
|
'token' => $this->token, |
217
|
|
|
'date' => $this->date->format('c'), |
218
|
|
|
'status' => $this->status, |
219
|
|
|
'statusMessage' => $this->statusMessage, |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.