|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Openpp\NotificationHubsRest\Registration; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AbstractRegistration implements RegistrationInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \DomDocument |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $dom; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \DOMNode |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $content; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $token; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string|string[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $tags; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $template; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $registrationId; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $eTag; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Initializes a new Registration. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->dom = new \DomDocument('1.0', 'utf-8'); |
|
48
|
|
|
|
|
49
|
|
|
$entryElement = $this->dom->createElement('entry'); |
|
50
|
|
|
$entryElement->setAttribute('xmlns', 'http://www.w3.org/2005/Atom'); |
|
51
|
|
|
|
|
52
|
|
|
$contentElement = $this->dom->createElement('content'); |
|
53
|
|
|
$contentElement->setAttribute('type', 'application/xml'); |
|
54
|
|
|
|
|
55
|
|
|
$this->content = $this->dom |
|
56
|
|
|
->appendChild($entryElement) |
|
57
|
|
|
->appendChild($contentElement) |
|
58
|
|
|
; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Sets Gcm Registration ID or APNs Device token. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $token |
|
65
|
|
|
* |
|
66
|
|
|
* @return RegistrationInterface this object |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setToken($token) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->token = $token; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Sets the tags. |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $tags |
|
79
|
|
|
* |
|
80
|
|
|
* @return RegistrationInterface this object |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setTags($tags) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->tags = $tags; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Sets the template for the body. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $template |
|
93
|
|
|
* |
|
94
|
|
|
* @return RegistrationInterface this object |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setTemplate($template) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->template = $template; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets the registration ID for update or delete registration API. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $registrationId |
|
107
|
|
|
* |
|
108
|
|
|
* @return RegistrationInterface this object |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setRegistrationId($registrationId) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->registrationId = $registrationId; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the token. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getToken() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->token; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the registration ID. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getRegistrationId() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->registrationId; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sets the ETag for update or delete registration API. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $eTag |
|
141
|
|
|
* |
|
142
|
|
|
* @return RegistrationInterface this object |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setETag($eTag) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->eTag = $eTag; |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the ETag. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getETag() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->eTag; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
*/ |
|
164
|
|
|
public function buildUri($endpoint, $hubPath) |
|
165
|
|
|
{ |
|
166
|
|
|
$uri = $endpoint.$hubPath.'/registrations/'; |
|
167
|
|
|
|
|
168
|
|
|
if ($this->registrationId) { |
|
169
|
|
|
$uri .= $this->registrationId; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $uri; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* {@inheritdoc} |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getContentType() |
|
179
|
|
|
{ |
|
180
|
|
|
return 'application/atom+xml;type=entry;charset=utf-8'; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* {@inheritdoc} |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getHeaders() |
|
187
|
|
|
{ |
|
188
|
|
|
$headers = [ |
|
189
|
|
|
'Content-Type: '.$this->getContentType(), |
|
190
|
|
|
'x-ms-version: '.'2013-08', |
|
191
|
|
|
]; |
|
192
|
|
|
|
|
193
|
|
|
if ($this->eTag) { |
|
194
|
|
|
$headers[] = 'If-Match: '.$this->eTag; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $headers; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns the atom payload for the registration request. |
|
202
|
|
|
* |
|
203
|
|
|
* @throws \RuntimeException |
|
204
|
|
|
* |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getPayload() |
|
208
|
|
|
{ |
|
209
|
|
|
if (!$this->token) { |
|
210
|
|
|
throw new \RuntimeException('Token is mandatory.'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$descriptionNode = $this->appendDescriptionNode(); |
|
214
|
|
|
$this->appendTagNode($descriptionNode); |
|
215
|
|
|
$this->appendTokenNode($descriptionNode); |
|
216
|
|
|
$this->appendTemplateNode($descriptionNode); |
|
217
|
|
|
$this->appendAdditionalNode($descriptionNode); |
|
218
|
|
|
|
|
219
|
|
|
$this->dom->formatOutput = true; |
|
220
|
|
|
|
|
221
|
|
|
return $this->dom->saveXML(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritdoc} |
|
226
|
|
|
*/ |
|
227
|
|
|
public function scrapeResponse($response) |
|
228
|
|
|
{ |
|
229
|
|
|
$dom = new \DOMDocument(); |
|
230
|
|
|
$dom->loadXML($response); |
|
231
|
|
|
|
|
232
|
|
|
if ($this->template) { |
|
233
|
|
|
$descriptionTag = $this->getTemplateRegistrationDescriptionTag(); |
|
234
|
|
|
} else { |
|
235
|
|
|
$descriptionTag = $this->getRegistrationDescriptionTag(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$description = $dom->getElementsByTagName($descriptionTag)->item(0); |
|
239
|
|
|
if (!$description) { |
|
240
|
|
|
throw new \RuntimeException("Could not find '".$descriptionTag."' tag in the response: ".$response); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$result = []; |
|
244
|
|
|
foreach ($description->childNodes as $child) { |
|
245
|
|
|
if ('#text' != $child->nodeName) { |
|
246
|
|
|
$result[$child->nodeName] = $child->nodeValue; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
return $result; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Appends the registration description DOMNode. |
|
255
|
|
|
* |
|
256
|
|
|
* @return \DOMNode |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function appendDescriptionNode() |
|
259
|
|
|
{ |
|
260
|
|
|
if ($this->template) { |
|
261
|
|
|
$descriptionTag = $this->getTemplateRegistrationDescriptionTag(); |
|
262
|
|
|
} else { |
|
263
|
|
|
$descriptionTag = $this->getRegistrationDescriptionTag(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$descriptionElement = $this->dom->createElement($descriptionTag); |
|
267
|
|
|
$descriptionElement->setAttribute('xmlns:i', 'http://www.w3.org/2001/XMLSchema-instance'); |
|
268
|
|
|
$descriptionElement->setAttribute('xmlns', 'http://schemas.microsoft.com/netservices/2010/10/servicebus/connect'); |
|
269
|
|
|
|
|
270
|
|
|
return $this->content->appendChild($descriptionElement); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Appends the 'Tags' DOMNode. |
|
275
|
|
|
* |
|
276
|
|
|
* @param \DOMNode $descriptionNode |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function appendTagNode($descriptionNode) |
|
279
|
|
|
{ |
|
280
|
|
|
if ($this->tags) { |
|
281
|
|
|
$descriptionNode->appendChild( |
|
282
|
|
|
$this->dom->createElement('Tags', $this->getTags()) |
|
283
|
|
|
); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Appends the token DOMNode. |
|
289
|
|
|
* |
|
290
|
|
|
* @param \DOMNode $descriptionNode |
|
291
|
|
|
*/ |
|
292
|
|
|
protected function appendTokenNode($descriptionNode) |
|
293
|
|
|
{ |
|
294
|
|
|
$descriptionNode->appendChild($this->dom->createElement($this->getTokenTag(), $this->token)); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Appends the 'BodyTemplate' DOMNode. |
|
299
|
|
|
* |
|
300
|
|
|
* @param \DOMNode $descriptionNode |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function appendTemplateNode($descriptionNode) |
|
303
|
|
|
{ |
|
304
|
|
|
if ($this->template) { |
|
305
|
|
|
$cdata = $this->dom->createCDATASection($this->template); |
|
306
|
|
|
$bodyTemplateElement = $this->dom->createElement('BodyTemplate'); |
|
307
|
|
|
$bodyTemplateElement->appendChild($cdata); |
|
308
|
|
|
$descriptionNode->appendChild($bodyTemplateElement); |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Appends the additional DOMNode. |
|
314
|
|
|
* |
|
315
|
|
|
* @param \DOMNode $descriptionNode |
|
316
|
|
|
*/ |
|
317
|
|
|
protected function appendAdditionalNode($descriptionNode) |
|
318
|
|
|
{ |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Returns the tags. |
|
323
|
|
|
* |
|
324
|
|
|
* @return string |
|
325
|
|
|
*/ |
|
326
|
|
|
private function getTags() |
|
327
|
|
|
{ |
|
328
|
|
|
if (is_array($this->tags)) { |
|
329
|
|
|
return implode(',', $this->tags); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $this->tags; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|