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