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\Notification; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageInterface; |
13
|
|
|
use Zbox\UnifiedPush\Exception\MalformedNotificationException; |
14
|
|
|
use Zbox\UnifiedPush\Exception\DomainException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class NotificationBuilder |
18
|
|
|
* @package Zbox\UnifiedPush\Notification |
19
|
|
|
*/ |
20
|
|
|
class NotificationBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var PayloadHandlerInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $payloadHandlers; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MessageInterface |
29
|
|
|
*/ |
30
|
|
|
private $message; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \ArrayIterator |
34
|
|
|
*/ |
35
|
|
|
private $notificationCollection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* NotificationBuilder constructor. |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->notificationCollection = new \ArrayIterator(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param PayloadHandlerInterface $handler |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function addPayloadHandler(PayloadHandlerInterface $handler) |
50
|
|
|
{ |
51
|
|
|
$hash = spl_object_hash($handler); |
52
|
|
|
|
53
|
|
|
if (!isset($this->payloadHandlers[$hash])) { |
54
|
|
|
$this->payloadHandlers[$hash] = $handler; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return PayloadHandlerInterface[] |
62
|
|
|
*/ |
63
|
|
|
public function getPayloadHandlers() |
64
|
|
|
{ |
65
|
|
|
return $this->payloadHandlers; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \ArrayIterator |
70
|
|
|
*/ |
71
|
|
|
public function getNotificationCollection() |
72
|
|
|
{ |
73
|
|
|
return $this->notificationCollection; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generates number of notifications by message recipient count |
78
|
|
|
* and notification service limitations |
79
|
|
|
* |
80
|
|
|
* @param MessageInterface $message |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function buildNotifications(MessageInterface $message) |
84
|
|
|
{ |
85
|
|
|
$this->message = $message; |
86
|
|
|
|
87
|
|
|
$recipientQueue = new \SplQueue(); |
88
|
|
|
$recipientChunk = new \ArrayIterator(); |
89
|
|
|
|
90
|
|
|
foreach ($message->getRecipientDeviceCollection() as $recipient) { |
91
|
|
|
$recipientChunk->append($recipient); |
92
|
|
|
|
93
|
|
|
if ($recipientChunk->count() >= $message->getMaxRecipientsPerMessage()) { |
94
|
|
|
$recipientQueue->enqueue($recipientChunk); |
95
|
|
|
$recipientChunk = new \ArrayIterator(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($recipientChunk->count()) { |
100
|
|
|
$recipientQueue->enqueue($recipientChunk); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
while (!$recipientQueue->isEmpty()) { |
104
|
|
|
$notification = $this->createNotification($recipientQueue->dequeue()); |
105
|
|
|
$this->notificationCollection->append($notification); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns created notification |
113
|
|
|
* |
114
|
|
|
* @param \ArrayIterator $recipients |
115
|
|
|
* @return Notification |
116
|
|
|
* @throws MalformedNotificationException |
117
|
|
|
*/ |
118
|
|
|
private function createNotification(\ArrayIterator $recipients) |
119
|
|
|
{ |
120
|
|
|
$message = clone $this->message; |
121
|
|
|
$message->setRecipientDeviceCollection($recipients); |
122
|
|
|
|
123
|
|
|
$handlers = $this->getPayloadHandlers(); |
124
|
|
|
|
125
|
|
|
foreach ($handlers as $handler) { |
126
|
|
|
if ($handler->isSupported($message)) { |
127
|
|
|
$packedPayload = $this->handlePayload($handler, $message); |
128
|
|
|
$customData = $handler->getCustomNotificationData(); |
129
|
|
|
|
130
|
|
|
return |
131
|
|
|
(new Notification()) |
132
|
|
|
->setType($message->getMessageType()) |
133
|
|
|
->setRecipients($recipients) |
134
|
|
|
->setPayload($packedPayload) |
135
|
|
|
->setCustomNotificationData($customData) |
136
|
|
|
; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
throw new DomainException( |
141
|
|
|
sprintf( |
142
|
|
|
'Unhandled message type %s', |
143
|
|
|
$message->getMessageType() |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param PayloadHandlerInterface $handler |
150
|
|
|
* @param MessageInterface $message |
151
|
|
|
* @return string |
152
|
|
|
* @throws MalformedNotificationException |
153
|
|
|
*/ |
154
|
|
|
private function handlePayload(PayloadHandlerInterface $handler, MessageInterface $message) |
155
|
|
|
{ |
156
|
|
|
$handler->setMessage($message); |
157
|
|
|
|
158
|
|
|
$payload = $handler->createPayload(); |
159
|
|
|
$packedPayload = $handler->packPayload($payload); |
160
|
|
|
|
161
|
|
|
$this->validatePayload($handler, $packedPayload); |
162
|
|
|
|
163
|
|
|
return $packedPayload; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Check if maximum size allowed for a notification payload exceeded |
168
|
|
|
* |
169
|
|
|
* @param PayloadHandlerInterface $handler |
170
|
|
|
* @param string $payload |
171
|
|
|
* @throws MalformedNotificationException |
172
|
|
|
*/ |
173
|
|
|
private function validatePayload(PayloadHandlerInterface $handler, $payload) |
174
|
|
|
{ |
175
|
|
|
$message = $this->message; |
176
|
|
|
$messageType = $message->getMessageType(); |
177
|
|
|
|
178
|
|
|
$maxLength = $handler->getPayloadMaxLength(); |
179
|
|
|
|
180
|
|
|
if (strlen($payload) > $maxLength) { |
181
|
|
|
throw new MalformedNotificationException( |
182
|
|
|
sprintf( |
183
|
|
|
"The maximum size allowed for '%s' notification payload is %d bytes", |
184
|
|
|
$messageType, |
185
|
|
|
$maxLength |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|