|
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
|
|
|
* Collection of recipient devices |
|
37
|
|
|
* |
|
38
|
|
|
* @var \ArrayIterator |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $recipientCollection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $data |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $data = array()) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->recipientCollection = new \ArrayIterator(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($data as $key => $value) { |
|
50
|
|
|
if (!isset($this->$key)) { |
|
51
|
|
|
$this->badMethodCallException($key); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->{'set'.ucfirst($key)}($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Checks if recipient`s token is valid |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $token |
|
64
|
|
|
* @return bool |
|
65
|
|
|
* @throws InvalidArgumentException |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract public function validateRecipient($token); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return \ArrayIterator |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getRecipientDeviceCollection() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->recipientCollection; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \ArrayIterator $collection |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setRecipientDeviceCollection(\ArrayIterator $collection) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->recipientCollection = $collection; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param \ArrayIterator $collection |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addRecipientIdentifiers(\ArrayIterator $collection) |
|
93
|
|
|
{ |
|
94
|
|
|
$collection->rewind(); |
|
95
|
|
|
|
|
96
|
|
|
while ($collection->valid()) { |
|
97
|
|
|
$deviceIdentifier = $collection->current(); |
|
98
|
|
|
$this->addRecipient($deviceIdentifier); |
|
99
|
|
|
$collection->next(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $deviceIdentifier |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function addRecipient($deviceIdentifier) |
|
110
|
|
|
{ |
|
111
|
|
|
$device = new RecipientDevice($deviceIdentifier, $this); |
|
112
|
|
|
$this->recipientCollection->append($device); |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Gets number of recipients allowed for single notification |
|
119
|
|
|
* |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getMaxRecipientsPerMessage() |
|
123
|
|
|
{ |
|
124
|
|
|
return static::MAX_RECIPIENTS_PER_MESSAGE_COUNT; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return \DateTime |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getExpirationTime() |
|
131
|
|
|
{ |
|
132
|
|
|
if (!$this->expirationTime) { |
|
133
|
|
|
$this->setExpirationTime(new \DateTime(self::DEFAULT_EXPIRATION_TIME_MODIFIER)); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->expirationTime; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param \DateTime $expirationTime |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setExpirationTime(\DateTime $expirationTime) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->expirationTime = DateTimeHelper::updateTimezoneToUniversal($expirationTime); |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Bad method call exception |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $name |
|
152
|
|
|
* @throws BadMethodCallException |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function badMethodCallException($name) |
|
155
|
|
|
{ |
|
156
|
|
|
throw new BadMethodCallException( |
|
157
|
|
|
sprintf("Unknown property '%s' of notification type '%s'.", $name, get_class($this)) |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Invalid argument exception |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $parameterName |
|
165
|
|
|
* @param string $expectedType |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function invalidArgumentException($parameterName, $expectedType) |
|
168
|
|
|
{ |
|
169
|
|
|
throw new InvalidArgumentException( |
|
170
|
|
|
sprintf( |
|
171
|
|
|
"Value type of '%s'::'%s' parameter is '%s' of notification type '%s'.", |
|
172
|
|
|
get_class($this), |
|
173
|
|
|
$parameterName, |
|
174
|
|
|
$expectedType |
|
175
|
|
|
) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Error handler for unknown property notification |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
*/ |
|
184
|
|
|
public function __get($name) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->badMethodCallException($name); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Error handler for unknown property of notification |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $name |
|
193
|
|
|
* @param mixed $value |
|
194
|
|
|
*/ |
|
195
|
|
|
public function __set($name, $value) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->badMethodCallException($name); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|