1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\Message; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Exception\BadMethodCallException, |
13
|
|
|
Zbox\UnifiedPush\Exception\InvalidArgumentException; |
14
|
|
|
use Zbox\UnifiedPush\Utils\DateTimeHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MessageBase |
18
|
|
|
* @package Zbox\UnifiedPush\Message |
19
|
|
|
*/ |
20
|
|
|
abstract class MessageBase implements MessageInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Default modifier (a date/time string) |
24
|
|
|
*/ |
25
|
|
|
const DEFAULT_EXPIRATION_TIME_MODIFIER = '4 weeks'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A UNIX epoch date expressed in seconds (UTC) that identifies |
29
|
|
|
* when the notification is no longer valid and can be discarded |
30
|
|
|
* |
31
|
|
|
* @var \DateTime |
32
|
|
|
*/ |
33
|
|
|
protected $expirationTime; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* An arbitrary, opaque value that identifies this notification. |
37
|
|
|
* This identifier is used for reporting errors to your server |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $messageIdentifier; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Collection of recipient devices |
45
|
|
|
* |
46
|
|
|
* @var \ArrayIterator |
47
|
|
|
*/ |
48
|
|
|
protected $recipientCollection; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $data |
52
|
|
|
*/ |
53
|
|
|
public function __construct(array $data = array()) |
54
|
|
|
{ |
55
|
|
|
$this->setMessageIdentifier(uniqid()); |
56
|
|
|
$this->recipientCollection = new \ArrayIterator(); |
57
|
|
|
|
58
|
|
|
foreach ($data as $key => $value) { |
59
|
|
|
if (!isset($this->$key)) { |
60
|
|
|
$this->badMethodCallException($key); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->{'set'.ucfirst($key)}($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Checks if recipient`s token is valid |
71
|
|
|
* |
72
|
|
|
* @param string $token |
73
|
|
|
* @return bool |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
abstract public function validateRecipient($token); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \ArrayIterator |
80
|
|
|
*/ |
81
|
|
|
public function getRecipientDeviceCollection() |
82
|
|
|
{ |
83
|
|
|
return $this->recipientCollection; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \ArrayIterator $collection |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setRecipientDeviceCollection(\ArrayIterator $collection) |
91
|
|
|
{ |
92
|
|
|
$this->recipientCollection = $collection; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \ArrayIterator $collection |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function addRecipientIdentifiers(\ArrayIterator $collection) |
102
|
|
|
{ |
103
|
|
|
while ($collection->valid()) { |
104
|
|
|
$deviceIdentifier = $collection->current(); |
105
|
|
|
$this->addRecipient($deviceIdentifier); |
106
|
|
|
$collection->next(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $deviceIdentifier |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function addRecipient($deviceIdentifier) |
117
|
|
|
{ |
118
|
|
|
$device = new RecipientDevice($deviceIdentifier, $this); |
119
|
|
|
$this->recipientCollection->append($device); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Gets number of recipients allowed for single notification |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function getMaxRecipientsPerMessage() |
130
|
|
|
{ |
131
|
|
|
return static::MAX_RECIPIENTS_PER_MESSAGE_COUNT; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return \DateTime |
136
|
|
|
*/ |
137
|
|
|
public function getExpirationTime() |
138
|
|
|
{ |
139
|
|
|
if (!$this->expirationTime) { |
140
|
|
|
$this->setExpirationTime(new \DateTime(self::DEFAULT_EXPIRATION_TIME_MODIFIER)); |
141
|
|
|
} |
142
|
|
|
return $this->expirationTime; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \DateTime $expirationTime |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setExpirationTime(\DateTime $expirationTime) |
150
|
|
|
{ |
151
|
|
|
$this->expirationTime = DateTimeHelper::updateTimezoneToUniversal($expirationTime); |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getMessageIdentifier() |
159
|
|
|
{ |
160
|
|
|
return $this->messageIdentifier; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $messageIdentifier |
165
|
|
|
* @throws InvalidArgumentException |
166
|
|
|
*/ |
167
|
|
|
public function setMessageIdentifier($messageIdentifier) |
168
|
|
|
{ |
169
|
|
|
if (!is_scalar($messageIdentifier)) { |
170
|
|
|
throw new InvalidArgumentException("Message identifier must be a scalar value"); |
171
|
|
|
} |
172
|
|
|
$this->messageIdentifier = $messageIdentifier; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Bad method call exception |
177
|
|
|
* |
178
|
|
|
* @param string $name |
179
|
|
|
* @throws BadMethodCallException |
180
|
|
|
*/ |
181
|
|
|
protected function badMethodCallException($name) |
182
|
|
|
{ |
183
|
|
|
throw new BadMethodCallException( |
184
|
|
|
sprintf("Unknown property '%s' of notification type '%s'.", $name, get_class($this)) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Invalid argument exception |
190
|
|
|
* |
191
|
|
|
* @param string $parameterName |
192
|
|
|
* @param string $expectedType |
193
|
|
|
*/ |
194
|
|
|
protected function invalidArgumentException($parameterName, $expectedType) |
195
|
|
|
{ |
196
|
|
|
throw new InvalidArgumentException( |
197
|
|
|
sprintf( |
198
|
|
|
"Value type of '%s'::'%s' parameter is '%s' of notification type '%s'.", |
199
|
|
|
get_class($this), |
200
|
|
|
$parameterName, |
201
|
|
|
$expectedType |
202
|
|
|
) |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Error handler for unknown property notification |
208
|
|
|
* |
209
|
|
|
* @param string $name |
210
|
|
|
*/ |
211
|
|
|
public function __get($name) |
212
|
|
|
{ |
213
|
|
|
$this->badMethodCallException($name); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Error handler for unknown property of notification |
218
|
|
|
* |
219
|
|
|
* @param string $name |
220
|
|
|
* @param mixed $value |
221
|
|
|
*/ |
222
|
|
|
public function __set($name, $value) |
223
|
|
|
{ |
224
|
|
|
$this->badMethodCallException($name); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|