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 ( 594733...71c569 )
by Alexander
02:23
created

NotificationBuilder::buildNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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
15
/**
16
 * Class NotificationBuilder
17
 * @package Zbox\UnifiedPush\Notification
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 Notification|null
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
        $recipientChunk = new \ArrayIterator();
67
68
        while ($recipient = $message->getRecipientDevice()) {
69
            $recipientChunk->append($recipient);
70
71
            if ($recipientChunk->count() >= $message->getMaxRecipientsPerMessage()) {
72
                $recipientQueue->enqueue($recipientChunk);
73
                $recipientChunk = new \ArrayIterator();
74
            }
75
        }
76
77
        if ($recipientChunk->count()) {
78
            $recipientQueue->enqueue($recipientChunk);
79
        }
80
81
        while (!$recipientQueue->isEmpty()) {
82
            $message->setRecipientCollection($recipientQueue->dequeue());
83
            $notification = $this->buildNotification();
84
            $this->notifications->append($notification);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Returns created notification
92
     *
93
     * @return Notification
94
     */
95
    private function buildNotification()
96
    {
97
        $message         = $this->message;
98
        $payload         = $message->createPayload();
99
        $recipients      = $message->getRecipientCollection();
100
101
        $packedPayload = $message->packPayload($payload);
102
        $this->validatePayload($packedPayload);
103
104
        return
105
            (new Notification())
106
                ->setPayload($packedPayload)
107
                ->setRecipients($recipients)
108
                ->setMessage($message)
109
            ;
110
    }
111
112
    /**
113
     * Check if maximum size allowed for a notification payload exceeded
114
     *
115
     * @param string $payload
116
     * @throws MalformedNotificationException
117
     */
118
    protected function validatePayload($payload)
119
    {
120
        $message     = $this->message;
121
        $maxLength   = $message->getPayloadMaxLength();
122
        $messageType = $message->getMessageType();
123
124
        if (strlen($payload) > $maxLength) {
125
            throw new MalformedNotificationException(
126
                sprintf(
127
                    "The maximum size allowed for '%s' notification payload is %d bytes",
128
                    $messageType,
129
                    $maxLength
130
                )
131
            );
132
        }
133
    }
134
}
135