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
|
|
|
* @param PayloadHandlerInterface $handler |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function addPayloadHandler(PayloadHandlerInterface $handler) |
32
|
|
|
{ |
33
|
|
|
$hash = spl_object_hash($handler); |
34
|
|
|
|
35
|
|
|
if (!isset($this->payloadHandlers[$hash])) { |
36
|
|
|
$this->payloadHandlers[$hash] = $handler; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return PayloadHandlerInterface[] |
44
|
|
|
*/ |
45
|
|
|
public function getPayloadHandlers() |
46
|
|
|
{ |
47
|
|
|
return $this->payloadHandlers; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generates number of notifications by message recipient count |
52
|
|
|
* and notification service limitations |
53
|
|
|
* |
54
|
|
|
* @param MessageInterface $message |
55
|
|
|
* @return \ArrayIterator |
56
|
|
|
*/ |
57
|
|
|
public function buildNotifications(MessageInterface $message) |
58
|
|
|
{ |
59
|
|
|
$notifications = new \ArrayIterator(); |
60
|
|
|
$recipientQueue = new \SplQueue(); |
61
|
|
|
$recipientChunk = new \ArrayIterator(); |
62
|
|
|
|
63
|
|
|
foreach ($message->getRecipientDeviceCollection() as $recipient) { |
64
|
|
|
$recipientChunk->append($recipient); |
65
|
|
|
|
66
|
|
|
if ($recipientChunk->count() >= $message->getMaxRecipientsPerMessage()) { |
67
|
|
|
$recipientQueue->enqueue($recipientChunk); |
68
|
|
|
$recipientChunk = new \ArrayIterator(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($recipientChunk->count()) { |
73
|
|
|
$recipientQueue->enqueue($recipientChunk); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
while (!$recipientQueue->isEmpty()) { |
77
|
|
|
$notification = $this->createNotification($recipientQueue->dequeue(), $message); |
78
|
|
|
$notifications->append($notification); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $notifications; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns created notification |
86
|
|
|
* |
87
|
|
|
* @param \ArrayIterator $recipients |
88
|
|
|
* @param MessageInterface $message |
89
|
|
|
* @return Notification |
90
|
|
|
*/ |
91
|
|
|
private function createNotification(\ArrayIterator $recipients, MessageInterface $message) |
92
|
|
|
{ |
93
|
|
|
$message = clone $message; |
94
|
|
|
$message->setRecipientDeviceCollection($recipients); |
95
|
|
|
|
96
|
|
|
$handlers = $this->getPayloadHandlers(); |
97
|
|
|
|
98
|
|
|
foreach ($handlers as $handler) { |
99
|
|
|
if ($handler->isSupported($message)) { |
100
|
|
|
$notificationId = uniqid(); |
101
|
|
|
$handler |
102
|
|
|
->setNotificationId($notificationId) |
103
|
|
|
->setMessage($message); |
104
|
|
|
|
105
|
|
|
$packedPayload = $this->handlePayload($handler); |
106
|
|
|
$customData = $handler->getCustomNotificationData(); |
107
|
|
|
|
108
|
|
|
$notification = new Notification(); |
109
|
|
|
$notification |
110
|
|
|
->setIdentifier($notificationId) |
111
|
|
|
->setType($message->getMessageType()) |
112
|
|
|
->setRecipients($recipients) |
113
|
|
|
->setPayload($packedPayload) |
114
|
|
|
->setCustomNotificationData($customData) |
115
|
|
|
; |
116
|
|
|
return $notification; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new DomainException( |
121
|
|
|
sprintf( |
122
|
|
|
'Unhandled message type %s', |
123
|
|
|
$message->getMessageType() |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param PayloadHandlerInterface $handler |
130
|
|
|
* @return string |
131
|
|
|
* @throws MalformedNotificationException |
132
|
|
|
*/ |
133
|
|
|
private function handlePayload(PayloadHandlerInterface $handler) |
134
|
|
|
{ |
135
|
|
|
$payload = $handler->createPayload(); |
136
|
|
|
$packedPayload = $handler->packPayload($payload); |
137
|
|
|
|
138
|
|
|
$handler->validatePayload($packedPayload); |
139
|
|
|
|
140
|
|
|
return $packedPayload; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|