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; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Exception\MalformedNotificationException; |
13
|
|
|
use Zbox\UnifiedPush\Utils\JsonEncoder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class NotificationBuilder |
17
|
|
|
* @package Zbox\UnifiedPush\Message |
18
|
|
|
*/ |
19
|
|
|
class NotificationBuilder |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MessageInterface |
23
|
|
|
*/ |
24
|
|
|
private $message; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \ArrayIterator |
28
|
|
|
*/ |
29
|
|
|
private $notifications; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param MessageInterface $message |
33
|
|
|
*/ |
34
|
|
|
public function __construct(MessageInterface $message) |
35
|
|
|
{ |
36
|
|
|
$this->notifications = new \ArrayIterator(); |
37
|
|
|
$this->message = $message; |
38
|
|
|
$this->buildNotifications(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getNotification() |
45
|
|
|
{ |
46
|
|
|
$collection = $this->notifications; |
47
|
|
|
|
48
|
|
|
if ($collection->valid()) { |
49
|
|
|
$notification = $collection->current(); |
50
|
|
|
$collection->next(); |
51
|
|
|
return $notification; |
52
|
|
|
} |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Generates number of notifications by message recipient count |
58
|
|
|
* and notification service limitations |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function buildNotifications() |
63
|
|
|
{ |
64
|
|
|
$message = $this->message; |
65
|
|
|
$recipientQueue = new \SplQueue(); |
66
|
|
|
|
67
|
|
|
while ($recipient = $message->getRecipient()) { |
68
|
|
|
$recipientChunk[] = $recipient; |
|
|
|
|
69
|
|
|
|
70
|
|
|
if (count($recipientChunk) >= $message->getMaxRecipientsPerMessage()) { |
71
|
|
|
$recipientQueue->enqueue($recipientChunk); |
|
|
|
|
72
|
|
|
$recipientChunk = []; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!empty($recipientChunk)) { |
77
|
|
|
$recipientQueue->enqueue($recipientChunk); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
while (!$recipientQueue->isEmpty()) { |
81
|
|
|
$notification = $this->buildNotification($recipientQueue->dequeue()); |
82
|
|
|
$this->notifications->append($notification); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns validated and encoded message |
90
|
|
|
* |
91
|
|
|
* @param array $recipients |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
private function buildNotification($recipients) |
95
|
|
|
{ |
96
|
|
|
$message = $this->message; |
97
|
|
|
$messageData = $message->createMessage($recipients); |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (is_array($messageData)) { |
100
|
|
|
$messageData = JsonEncoder::jsonEncode($messageData); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->validatePayload($messageData); |
104
|
|
|
|
105
|
|
|
$notification = $message->packMessage($messageData, $recipients); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $notification; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if maximum size allowed for a notification payload exceeded |
112
|
|
|
* |
113
|
|
|
* @param string $payload |
114
|
|
|
* @throws MalformedNotificationException |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function validatePayload($payload) |
118
|
|
|
{ |
119
|
|
|
$message = $this->message; |
120
|
|
|
$maxLength = $message->getPayloadMaxLength(); |
121
|
|
|
$messageType = $message->getMessageType(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
if (strlen($payload) > $maxLength) { |
124
|
|
|
throw new MalformedNotificationException( |
125
|
|
|
sprintf( |
126
|
|
|
"The maximum size allowed for '%s' notification payload is %d bytes", |
127
|
|
|
$messageType, |
128
|
|
|
$maxLength |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.