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 ( 15375f...2893b6 )
by Alexander
02:22
created

Application::addInvalidRecipient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 19
rs 9.4285
cc 3
eloc 10
nc 3
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;
11
12
use Zbox\UnifiedPush\Message\MessageInterface,
13
    Zbox\UnifiedPush\Message\RecipientDevice;
14
use Zbox\UnifiedPush\NotificationService\NotificationServices;
15
use Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsMapper;
16
use Zbox\UnifiedPush\Exception\DomainException,
17
    Zbox\UnifiedPush\Exception\InvalidArgumentException;
18
19
/**
20
 * Class Application
21
 * @package Zbox\UnifiedPush
22
 */
23
class Application
24
{
25
    /**
26
     * @var CredentialsMapper
27
     */
28
    private $credentialsMapper;
29
30
    /**
31
     * @var string
32
     */
33
    private $credentialsFilePath;
34
35
    /**
36
     * @var string
37
     */
38
    private $applicationName;
39
40
    /**
41
     * @var array
42
     */
43
    private $config;
44
45
    /**
46
     * @var \ArrayObject
47
     */
48
    private $messages;
49
50
    /**
51
     * @var \ArrayIterator
52
     */
53
    private $invalidRecipients;
54
55
    /**
56
     * @param string $applicationName
57
     * @param CredentialsMapper $credentialsMapper
58
     */
59
    public function __construct($applicationName, CredentialsMapper $credentialsMapper)
60
    {
61
        $this->setApplicationName($applicationName);
62
        $this->credentialsMapper = $credentialsMapper;
63
        $this->messages = new \ArrayObject();
64
65
        return $this;
66
    }
67
68
    /**
69
     * Returns path to application-based notification services credentials
70
     *
71
     * @return string
72
     */
73
    public function getCredentialsFilePath()
74
    {
75
        if (!file_exists($this->credentialsFilePath)) {
76
            throw new InvalidArgumentException(
77
                sprintf(
78
                    "Application credentials file '%s' not exists",
79
                    $this->credentialsFilePath
80
                )
81
            );
82
        }
83
        return $this->credentialsFilePath;
84
    }
85
86
    /**
87
     * @param string $filePath
88
     * @return $this
89
     */
90
    public function setCredentialsFilePath($filePath)
91
    {
92
        $this->credentialsFilePath = $filePath;
93
        return $this;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getApplicationName()
100
    {
101
        return $this->applicationName;
102
    }
103
104
    /**
105
     * @param string $applicationName
106
     * @return $this
107
     */
108
    public function setApplicationName($applicationName)
109
    {
110
        $this->applicationName = $applicationName;
111
        return $this;
112
    }
113
114
    /**
115
     * Gets an iterator from an ArrayObject instance
116
     *
117
     * @return \ArrayIterator
118
     */
119
    public function getMessagesIterator()
120
    {
121
        return $this->messages->getIterator();
122
    }
123
124
    /**
125
     * Adds a message to collection
126
     *
127
     * @param MessageInterface $message
128
     * @return $this
129
     */
130
    public function addMessage(MessageInterface $message)
131
    {
132
        $messageId = $message->getMessageIdentifier();
133
        $this->messages->offsetSet($messageId, $message);
134
        return $this;
135
    }
136
137
    /**
138
     * Removes the given message from collection
139
     *
140
     * @param MessageInterface $message
141
     * @return $this
142
     */
143
    public function unsetMessage(MessageInterface $message)
144
    {
145
        $messageId = $message->getMessageIdentifier();
146
        $this->messages->offsetUnset($messageId);
147
        return $this;
148
    }
149
150
    /**
151
     * @return \ArrayIterator
152
     */
153
    public function getInvalidRecipients()
154
    {
155
        return $this->invalidRecipients;
156
    }
157
158
    /**
159
     * Adds a invalid recipient to collection
160
     *
161
     * @param string $serviceName
162
     * @param RecipientDevice|string $recipient
163
     * @return $this
164
     */
165
    public function addInvalidRecipient($serviceName, $recipient)
166
    {
167
        if (!in_array($serviceName, $this->getInitializedServices())) {
168
            throw new DomainException(
169
                sprintf("Recipient of an unsupported service '%s'", $serviceName)
170
            );
171
        }
172
173
        if (is_string($recipient)) {
174
            $messageClassName = 'Zbox\UnifiedPush\Message\Type\\' . $serviceName;
175
            $recipient        = new RecipientDevice($recipient, $messageClassName());
176
177
            $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED);
178
        }
179
180
        $this->invalidRecipients->append($recipient);
181
182
        return $this;
183
    }
184
185
    /**
186
     * Returns the list of names of notification services available for the application
187
     *
188
     * @return array
189
     */
190
    public function getInitializedServices()
191
    {
192
        return array_keys($this->config);
193
    }
194
195
    /**
196
     * Returns credentials for notification service
197
     *
198
     * @param string $serviceName
199
     * @throws DomainException
200
     * @return array
201
     */
202
    public function getCredentialsByService($serviceName)
203
    {
204
        if ($this->getApplicationName() && empty($this->config)) {
205
            $this->loadApplicationConfig();
206
        }
207
208
        if (!in_array($serviceName, $this->getInitializedServices())) {
209
            throw new DomainException(
210
                sprintf("Credentials for service '%s' was not initialized", $serviceName)
211
            );
212
        }
213
214
        $credentials = NotificationServices::getCredentialsTypeByService($serviceName);
215
216
        return
217
            $this
218
                ->credentialsMapper
219
                ->mapCredentials(
220
                    $credentials,
221
                    $this->config[$serviceName]
222
                )
223
            ;
224
    }
225
226
    /**
227
     * Load sender`s notification services credentials by application name
228
     *
229
     * @return $this
230
     * @throws DomainException
231
     */
232
    public function loadApplicationConfig()
233
    {
234
        $configFilePath = $this->getCredentialsFilepath();
235
        $applicationsConfig = json_decode(file_get_contents($configFilePath), true);
236
237
        $applicationName = $this->getApplicationName();
238
239
        if (!array_key_exists($applicationName, $applicationsConfig)) {
240
            throw new DomainException(
241
                sprintf("Application '%s' is not defined.", $applicationName)
242
            );
243
        }
244
        $this->config = $applicationsConfig[$applicationName];
245
246
        return $this;
247
    }
248
}
249