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 ( c42bc4...743bf3 )
by Alexander
02:32
created

NotificationBuilder::validatePayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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