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 ( 51a94a...8405be )
by Alexander
03:01
created

Application::setCredentialsFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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;
11
12
use Zbox\UnifiedPush\Message\MessageInterface,
13
    Zbox\UnifiedPush\Message\RecipientDevice;
14
use Zbox\UnifiedPush\Exception\DomainException,
15
    Zbox\UnifiedPush\Exception\InvalidArgumentException;
16
17
/**
18
 * Class Application
19
 * @package Zbox\UnifiedPush
20
 */
21
class Application
22
{
23
    const APPS_CREDENTIALS_FILENAME = 'applications_credentials.json';
24
25
    /**
26
     * @var string
27
     */
28
    private $credentialsFilePath;
29
30
    /**
31
     * @var string
32
     */
33
    private $applicationName;
34
35
    /**
36
     * @var array
37
     */
38
    private $config;
39
40
    /**
41
     * @var \ArrayObject
42
     */
43
    private $messages;
44
45
    /**
46
     * @var \ArrayIterator
47
     */
48
    private $invalidRecipients;
49
50
    /**
51
     * @param string $applicationName
52
     */
53
    public function __construct($applicationName)
54
    {
55
        $this->setApplicationName($applicationName);
56
        $this->messages = new \ArrayObject();
57
58
        return $this;
59
    }
60
61
    /**
62
     * Returns path to application-based notification services credentials
63
     *
64
     * @return string
65
     */
66
    public function getCredentialsFilePath()
67
    {
68
        if (!file_exists($this->credentialsFilePath)) {
69
            $credentialsDistFilename = sprintf('%s.dist', static::APPS_CREDENTIALS_FILENAME);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $credentialsDistFilename exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
70
71
            throw new InvalidArgumentException(
72
                sprintf(
73
                    "Application credentials file '%s' not exists. See example %s",
74
                    $this->credentialsFilePath,
75
                    $credentialsDistFilename
76
                )
77
            );
78
        }
79
        return $this->credentialsFilePath;
80
    }
81
82
    /**
83
     * @param string $filePath
84
     * @return $this
85
     */
86
    public function setCredentialsFilePath($filePath)
87
    {
88
        $this->credentialsFilePath = $filePath;
89
        return $this;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getApplicationName()
96
    {
97
        return $this->applicationName;
98
    }
99
100
    /**
101
     * @param string $applicationName
102
     * @return $this
103
     */
104
    public function setApplicationName($applicationName)
105
    {
106
        $this->applicationName = $applicationName;
107
        return $this;
108
    }
109
110
    /**
111
     * Gets an iterator from an ArrayObject instance
112
     *
113
     * @return \ArrayIterator
114
     */
115
    public function getMessagesIterator()
116
    {
117
        return $this->messages->getIterator();
118
    }
119
120
    /**
121
     * Adds a message to collection
122
     *
123
     * @param MessageInterface $message
124
     * @return $this
125
     */
126
    public function addMessage(MessageInterface $message)
127
    {
128
        $messageId = $message->getMessageIdentifier();
129
        $this->messages->offsetSet($messageId, $message);
130
        return $this;
131
    }
132
133
    /**
134
     * Removes the given message from collection
135
     *
136
     * @param MessageInterface $message
137
     * @return $this
138
     */
139
    public function unsetMessage(MessageInterface $message)
140
    {
141
        $messageId = $message->getMessageIdentifier();
142
        $this->messages->offsetUnset($messageId);
143
        return $this;
144
    }
145
146
    /**
147
     * @return \ArrayIterator
148
     */
149
    public function getInvalidRecipients()
150
    {
151
        return $this->invalidRecipients;
152
    }
153
154
    /**
155
     * Adds a invalid recipient to collection
156
     *
157
     * @param string $serviceName
158
     * @param RecipientDevice|string $recipient
159
     * @return $this
160
     */
161
    public function addInvalidRecipient($serviceName, $recipient)
162
    {
163
        if (!in_array($serviceName, $this->getInitializedServices())) {
164
            throw new DomainException(
165
                sprintf("Recipient of an unsupported service '%s'", $serviceName)
166
            );
167
        }
168
169
        if (is_string($recipient)) {
170
            $messageClassName = 'Zbox\UnifiedPush\Message\Type\\' . $serviceName;
171
            $recipient        = new RecipientDevice($recipient, $messageClassName());
172
173
            $recipient->setIdentifierStatus(RecipientDevice::DEVICE_NOT_REGISTERED);
174
        }
175
176
        $this->invalidRecipients->append($recipient);
177
178
        return $this;
179
    }
180
181
    /**
182
     * Returns the list of names of notification services available for the application
183
     *
184
     * @return array
185
     */
186
    public function getInitializedServices()
187
    {
188
        return array_keys($this->config);
189
    }
190
191
    /**
192
     * Returns credentials for notification service
193
     *
194
     * @param string $serviceName
195
     * @throws DomainException
196
     * @return string
197
     */
198
    public function getCredentialsByService($serviceName)
199
    {
200
        if ($this->getApplicationName() && !$this->config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
201
            $this->loadApplicationConfig();
202
        }
203
204
        if (!in_array($serviceName, $this->getInitializedServices())) {
205
            throw new DomainException(
206
                sprintf("Credentials for service '%s' was not initialized", $serviceName)
207
            );
208
        }
209
        return $this->config[$serviceName];
210
    }
211
212
    /**
213
     * Load sender`s notification services credentials by application name
214
     *
215
     * @return $this
216
     * @throws DomainException
217
     */
218
    public function loadApplicationConfig()
219
    {
220
        $configFilePath = $this->getCredentialsFilepath();
221
        $applicationsConfig = json_decode(file_get_contents($configFilePath), true);
222
223
        $applicationName = $this->getApplicationName();
224
225
        if (!array_key_exists($applicationName, $applicationsConfig)) {
226
            throw new DomainException(
227
                sprintf("Application '%s' is not defined.", $applicationName)
228
            );
229
        }
230
        $this->config = $applicationsConfig[$applicationName];
231
232
        return $this;
233
    }
234
}
235