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.
Passed
Branch master (98970a)
by Alexander
02:59 queued 21s
created

MessageBase::getRecipientDevice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
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\Message;
11
12
use Zbox\UnifiedPush\Exception\BadMethodCallException,
13
    Zbox\UnifiedPush\Exception\InvalidArgumentException;
14
use Zbox\UnifiedPush\Utils\DateTimeHelper;
15
16
/**
17
 * Class MessageBase
18
 * @package Zbox\UnifiedPush\Message
19
 */
20
abstract class MessageBase implements MessageInterface
21
{
22
    /**
23
     * Default modifier (a date/time string)
24
     */
25
    const DEFAULT_EXPIRATION_TIME_MODIFIER = '4 weeks';
26
27
    /**
28
     * A UNIX epoch date expressed in seconds (UTC) that identifies
29
     * when the notification is no longer valid and can be discarded
30
     *
31
     * @var \DateTime
32
     */
33
    protected $expirationTime;
34
35
    /**
36
     * An arbitrary, opaque value that identifies this notification.
37
     * This identifier is used for reporting errors to your server
38
     *
39
     * @var string
40
     */
41
    protected $messageIdentifier;
42
43
44
    /**
45
     * Collection of recipient devices
46
     *
47
     * @var \ArrayIterator
48
     */
49
    protected $recipientCollection;
50
51
    /**
52
     * @param array $data
53
     */
54
    public function __construct(array $data = array())
55
    {
56
        $this->setMessageIdentifier(uniqid());
57
        $this->recipientCollection = new \ArrayIterator();
58
59
        foreach ($data as $key => $value) {
60
            if (!isset($this->$key)) {
61
                $this->badMethodCallException($key);
62
            }
63
64
            $this->{'set'.ucfirst($key)}($value);
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * Checks if recipient`s token is valid
72
     *
73
     * @param string $token
74
     * @return bool
75
     * @throws InvalidArgumentException
76
     */
77
    abstract public function validateRecipient($token);
78
79
    /**
80
     * Gets number of recipients allowed for single notification
81
     *
82
     * @return int
83
     */
84
    public function getMaxRecipientsPerMessage()
85
    {
86
        return static::MAX_RECIPIENTS_PER_MESSAGE_COUNT;
87
    }
88
89
    /**
90
     * Gets maximum size allowed for notification payload
91
     *
92
     * @return int
93
     */
94
    public function getPayloadMaxLength()
95
    {
96
        return static::PAYLOAD_MAX_LENGTH;
97
    }
98
99
    /**
100
     * @return RecipientDevice
101
     */
102
    public function getRecipientDevice()
103
    {
104
        $collection = $this->recipientCollection;
105
106
        if ($collection->valid()) {
107
            $device = $collection->current();
108
            $collection->next();
109
            return $device;
110
        }
111
        return null;
112
    }
113
114
    /**
115
     * @return \ArrayIterator
116
     */
117
    public function getRecipientCollection()
118
    {
119
        return $this->recipientCollection;
120
    }
121
122
    /**
123
     * @param \ArrayIterator $collection
124
     * @return $this
125
     */
126
    public function setRecipientCollection(\ArrayIterator $collection)
127
    {
128
        $this->recipientCollection = $collection;
129
        return $this;
130
    }
131
132
    /**
133
     * @param \ArrayIterator $collection
134
     * @return $this
135
     */
136
    public function addRecipientIdentifiers(\ArrayIterator $collection)
137
    {
138
        while ($collection->valid()) {
139
            $deviceIdentifier = $collection->current();
140
            $this->addRecipient($deviceIdentifier);
141
            $collection->next();
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * @param string $deviceIdentifier
149
     * @return $this
150
     */
151
    public function addRecipient($deviceIdentifier)
152
    {
153
        $device = new RecipientDevice($deviceIdentifier, $this);
154
        $this->recipientCollection->append($device);
155
156
        return $this;
157
    }
158
159
    /**
160
     * @return \DateTime
161
     */
162
    public function getExpirationTime()
163
    {
164
        if (!$this->expirationTime) {
165
            $this->setExpirationTime(new \DateTime(self::DEFAULT_EXPIRATION_TIME_MODIFIER));
166
        }
167
        return $this->expirationTime;
168
    }
169
170
    /**
171
     * @param \DateTime $expirationTime
172
     * @return $this
173
     */
174
    public function setExpirationTime(\DateTime $expirationTime)
175
    {
176
        $this->expirationTime = DateTimeHelper::updateTimezoneToUniversal($expirationTime);
177
        return $this;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getMessageIdentifier()
184
    {
185
        return $this->messageIdentifier;
186
    }
187
188
    /**
189
     * @param string $messageIdentifier
190
     * @throws InvalidArgumentException
191
     */
192
    public function setMessageIdentifier($messageIdentifier)
193
    {
194
        if (!is_scalar($messageIdentifier)) {
195
            throw new InvalidArgumentException("Message identifier must be a scalar value");
196
        }
197
        $this->messageIdentifier = $messageIdentifier;
198
    }
199
200
    /**
201
     * Bad method call exception
202
     *
203
     * @param string $name
204
     * @throws BadMethodCallException
205
     */
206
    protected function badMethodCallException($name)
207
    {
208
        throw new BadMethodCallException(
209
            sprintf("Unknown property '%s' of notification type '%s'.", $name, get_class($this))
210
        );
211
    }
212
213
    /**
214
     * Invalid argument exception
215
     *
216
     * @param string $parameterName
217
     * @param string $expectedType
218
     */
219
    protected function invalidArgumentException($parameterName, $expectedType)
220
    {
221
        throw new InvalidArgumentException(
222
            sprintf(
223
                "Value type of '%s'::'%s' parameter is '%s' of notification type '%s'.",
224
                get_class($this),
225
                $parameterName,
226
                $expectedType
227
            )
228
        );
229
    }
230
231
    /**
232
     * Error handler for unknown property notification
233
     *
234
     * @param string $name
235
     */
236
    public function __get($name)
237
    {
238
        $this->badMethodCallException($name);
239
    }
240
241
    /**
242
     * Error handler for unknown property of notification
243
     *
244
     * @param string $name
245
     * @param mixed $value
246
     */
247
    public function __set($name, $value)
248
    {
249
        $this->badMethodCallException($name);
250
    }
251
}
252