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\NotificationService\NotificationServices; |
14
|
|
|
use Zbox\UnifiedPush\Exception\MalformedNotificationException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class NotificationBuilder |
18
|
|
|
* @package Zbox\UnifiedPush\Notification |
19
|
|
|
*/ |
20
|
|
|
class NotificationBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
private $payloadHandler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MessageInterface |
29
|
|
|
*/ |
30
|
|
|
private $message; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \ArrayIterator |
34
|
|
|
*/ |
35
|
|
|
private $notifications; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $type |
39
|
|
|
* @return PayloadHandlerInterface |
40
|
|
|
*/ |
41
|
|
|
public function getHandlerByService($type) |
42
|
|
|
{ |
43
|
|
|
NotificationServices::validateServiceName($type); |
44
|
|
|
|
45
|
|
|
if (empty($this->payloadHandler[$type])) { |
46
|
|
|
$handlerClass = sprintf('\Zbox\UnifiedPush\Notification\PayloadHandler\%s', $type); |
47
|
|
|
$this->payloadHandler[$type] = new $handlerClass(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->payloadHandler[$type]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Notification|null |
55
|
|
|
*/ |
56
|
|
|
public function getNotification() |
57
|
|
|
{ |
58
|
|
|
$collection = $this->notifications; |
59
|
|
|
|
60
|
|
|
if ($collection->valid()) { |
61
|
|
|
$notification = $collection->current(); |
62
|
|
|
$collection->next(); |
63
|
|
|
return $notification; |
64
|
|
|
} |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Generates number of notifications by message recipient count |
70
|
|
|
* and notification service limitations |
71
|
|
|
* |
72
|
|
|
* @param MessageInterface $message |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function buildNotifications(MessageInterface $message) |
76
|
|
|
{ |
77
|
|
|
$this->message = $message; |
78
|
|
|
|
79
|
|
|
$recipientQueue = new \SplQueue(); |
80
|
|
|
$recipientChunk = new \ArrayIterator(); |
81
|
|
|
|
82
|
|
|
foreach ($message->getRecipientDeviceCollection() as $recipient) { |
83
|
|
|
$recipientChunk->append($recipient); |
84
|
|
|
|
85
|
|
|
if ($recipientChunk->count() >= $message->getMaxRecipientsPerMessage()) { |
86
|
|
|
$recipientQueue->enqueue($recipientChunk); |
87
|
|
|
$recipientChunk = new \ArrayIterator(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($recipientChunk->count()) { |
92
|
|
|
$recipientQueue->enqueue($recipientChunk); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
while (!$recipientQueue->isEmpty()) { |
96
|
|
|
$notification = $this->createNotification($recipientQueue->dequeue()); |
97
|
|
|
$this->notifications->append($notification); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns created notification |
105
|
|
|
* |
106
|
|
|
* @param \ArrayIterator $recipients |
107
|
|
|
* @return Notification |
108
|
|
|
* @throws MalformedNotificationException |
109
|
|
|
*/ |
110
|
|
|
private function createNotification(\ArrayIterator $recipients) |
111
|
|
|
{ |
112
|
|
|
$message = clone $this->message; |
113
|
|
|
$message->setRecipientDeviceCollection($recipients); |
114
|
|
|
|
115
|
|
|
$payloadHandler = $this->getHandlerByService($message->getMessageType()); |
116
|
|
|
$payloadHandler->setMessage($message); |
117
|
|
|
|
118
|
|
|
$payload = $payloadHandler->createPayload(); |
119
|
|
|
$packedPayload = $payloadHandler->packPayload($payload); |
120
|
|
|
$customData = $payloadHandler->getCustomNotificationData(); |
121
|
|
|
|
122
|
|
|
$this->validatePayload($packedPayload); |
123
|
|
|
|
124
|
|
|
return |
125
|
|
|
(new Notification()) |
126
|
|
|
->setRecipients($recipients) |
127
|
|
|
->setPayload($packedPayload) |
128
|
|
|
->setCustomNotificationData($customData) |
129
|
|
|
; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if maximum size allowed for a notification payload exceeded |
134
|
|
|
* |
135
|
|
|
* @param string $payload |
136
|
|
|
* @throws MalformedNotificationException |
137
|
|
|
*/ |
138
|
|
|
private function validatePayload($payload) |
139
|
|
|
{ |
140
|
|
|
$message = $this->message; |
141
|
|
|
$messageType = $message->getMessageType(); |
142
|
|
|
|
143
|
|
|
$payloadHandler = $this->getHandlerByService($messageType); |
144
|
|
|
$maxLength = $payloadHandler->getPayloadMaxLength(); |
145
|
|
|
|
146
|
|
|
if (strlen($payload) > $maxLength) { |
147
|
|
|
throw new MalformedNotificationException( |
148
|
|
|
sprintf( |
149
|
|
|
"The maximum size allowed for '%s' notification payload is %d bytes", |
150
|
|
|
$messageType, |
151
|
|
|
$maxLength |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|