|
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\Type; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageBase; |
|
13
|
|
|
use Zbox\UnifiedPush\NotificationService\NotificationServices; |
|
14
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
|
15
|
|
|
use Zbox\UnifiedPush\Exception\BadMethodCallException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class MPNSBase |
|
19
|
|
|
* @package Zbox\UnifiedPush\Message\Type |
|
20
|
|
|
*/ |
|
21
|
|
|
class MPNSBase extends MessageBase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The maximum size allowed for MPNS message payload is 3K bytes |
|
25
|
|
|
*/ |
|
26
|
|
|
const PAYLOAD_MAX_LENGTH = 3072; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* MPNs does not support multicast sending |
|
30
|
|
|
*/ |
|
31
|
|
|
const MAX_RECIPIENTS_PER_MESSAGE_COUNT = 1; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Notification delivery interval |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $delayInterval; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getMessageType() |
|
44
|
|
|
{ |
|
45
|
|
|
return NotificationServices::MICROSOFT_PUSH_NOTIFICATIONS_SERVICE; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getMPNSType() |
|
52
|
|
|
{ |
|
53
|
|
|
return static::MESSAGE_TYPE; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* No expiration time available in MPN |
|
58
|
|
|
* |
|
59
|
|
|
* @throws BadMethodCallException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getExpirationTime() |
|
62
|
|
|
{ |
|
63
|
|
|
throw new BadMethodCallException("No expiration time available in MPN"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getDelayInterval() |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->delayInterval) { |
|
72
|
|
|
$this->setDelayInterval(static::DELAY_INTERVAL_IMMEDIATE); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->delayInterval; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param int $delayInterval |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setDelayInterval($delayInterval) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!in_array($delayInterval, array( |
|
84
|
|
|
static::DELAY_INTERVAL_IMMEDIATE, |
|
85
|
|
|
static::DELAY_INTERVAL_450, |
|
86
|
|
|
static::DELAY_INTERVAL_900 |
|
87
|
|
|
))) { |
|
88
|
|
|
throw new InvalidArgumentException('Delivery interval must be equal one of predefined interval flag'); |
|
89
|
|
|
} |
|
90
|
|
|
$this->delayInterval = $delayInterval; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \DOMDocument |
|
97
|
|
|
*/ |
|
98
|
|
|
public function createPayload() |
|
99
|
|
|
{ |
|
100
|
|
|
$messageType = ucfirst($this->getMPNSType()); |
|
101
|
|
|
|
|
102
|
|
|
$message = new \DOMDocument("1.0", "utf-8"); |
|
103
|
|
|
$baseElement = $message->createElement("wp:Notification"); |
|
104
|
|
|
$baseElement->setAttribute("xmlns:wp", "WPNotification"); |
|
105
|
|
|
$message->appendChild($baseElement); |
|
106
|
|
|
|
|
107
|
|
|
$rootElement = $message->createElement("wp:" . $messageType); |
|
108
|
|
|
$baseElement->appendChild($rootElement); |
|
109
|
|
|
|
|
110
|
|
|
foreach ($this->getPropertiesList() as $property) |
|
111
|
|
|
{ |
|
112
|
|
|
$propertyName = ucfirst($property->getName()); |
|
113
|
|
|
$getterName = 'get' . $propertyName; |
|
114
|
|
|
$value = $this->$getterName(); |
|
115
|
|
|
|
|
116
|
|
|
if ($value) { |
|
117
|
|
|
$name = "wp:" . $propertyName; |
|
118
|
|
|
$element = $message->createElement($name, $value); |
|
119
|
|
|
$rootElement->appendChild($element); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $message; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param \DOMDocument $payload |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function packPayload($payload) |
|
131
|
|
|
{ |
|
132
|
|
|
return $payload->saveXML(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function validateRecipient($token) |
|
139
|
|
|
{ |
|
140
|
|
|
if (base64_encode(base64_decode($token)) !== $token) { |
|
141
|
|
|
throw new InvalidArgumentException(sprintf( |
|
142
|
|
|
'Device token must be base64 string. Token given: "%s"', |
|
143
|
|
|
$token |
|
144
|
|
|
)); |
|
145
|
|
|
} |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function __toString() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->createPayload()->saveXML(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return \ReflectionProperty[] |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getPropertiesList() |
|
161
|
|
|
{ |
|
162
|
|
|
$reflection = new \ReflectionObject($this); |
|
163
|
|
|
$properties = $reflection->getProperties(\ReflectionProperty::IS_PRIVATE); |
|
164
|
|
|
|
|
165
|
|
|
return $properties; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|