GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ba3437...1dc585 )
by Alexander
02:23
created

NotificationBuilder::addPayloadHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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 $notifications;
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 Notification|null
54
     */
55
    public function getNotification()
56
    {
57
        $collection = $this->notifications;
58
59
        if ($collection->valid()) {
60
            $notification = $collection->current();
61
            $collection->next();
62
            return $notification;
63
        }
64
        return null;
65
    }
66
67
    /**
68
     * Generates number of notifications by message recipient count
69
     * and notification service limitations
70
     *
71
     * @param MessageInterface $message
72
     * @return $this
73
     */
74
    public function buildNotifications(MessageInterface $message)
75
    {
76
        $this->message        = $message;
77
        $this->notifications  = new \ArrayIterator();
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
        foreach ($this->payloadHandlers as $handler) {
116
            if ($handler->isSupported($message)) {
117
                $packedPayload  = $this->handlePayload($handler, $message);
118
                $customData     = $handler->getCustomNotificationData();
119
120
                return
121
                    (new Notification())
122
                        ->setRecipients($recipients)
123
                        ->setPayload($packedPayload)
124
                        ->setCustomNotificationData($customData)
125
                    ;
126
            }
127
        }
128
129
        throw new DomainException(
130
            sprintf(
131
                'Unhandled message type %s',
132
                $message->getMessageType()
133
            )
134
        );
135
    }
136
137
    /**
138
     * @param PayloadHandlerInterface $handler
139
     * @param MessageInterface $message
140
     * @return string
141
     * @throws MalformedNotificationException
142
     */
143
    private function handlePayload(PayloadHandlerInterface $handler, MessageInterface $message)
144
    {
145
        $handler->setMessage($message);
146
147
        $payload = $handler->createPayload();
148
        $packedPayload = $handler->packPayload($payload);
149
150
        $this->validatePayload($handler, $packedPayload);
151
152
        return $packedPayload;
153
    }
154
155
    /**
156
     * Check if maximum size allowed for a notification payload exceeded
157
     *
158
     * @param PayloadHandlerInterface $handler
159
     * @param string $payload
160
     * @throws MalformedNotificationException
161
     */
162
    private function validatePayload(PayloadHandlerInterface $handler, $payload)
163
    {
164
        $message     = $this->message;
165
        $messageType = $message->getMessageType();
166
167
        $maxLength   = $handler->getPayloadMaxLength();
168
169
        if (strlen($payload) > $maxLength) {
170
            throw new MalformedNotificationException(
171
                sprintf(
172
                    "The maximum size allowed for '%s' notification payload is %d bytes",
173
                    $messageType,
174
                    $maxLength
175
                )
176
            );
177
        }
178
    }
179
}
180