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 ( 74f330...e0d346 )
by Alexander
02:35
created

ServiceClientFactory::setDefaultConfigPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
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\NotificationService;
11
12
use Zbox\UnifiedPush\Exception\InvalidArgumentException;
13
use Zbox\UnifiedPush\Exception\RuntimeException;
14
use Zbox\UnifiedPush\Exception\DomainException;
15
16
/**
17
 * Class ServiceClientFactory
18
 * @package Zbox\UnifiedPush\NotificationService
19
 */
20
class ServiceClientFactory
21
{
22
    const ENVIRONMENT_PRODUCTION   = 'production';
23
    const ENVIRONMENT_DEVELOPMENT  = 'development';
24
25
    const PUSH_SERVICE             = 'push';
26
    const FEEDBACK_SERVICE         = 'feedback';
27
28
    /**
29
     * Type of environment
30
     *
31
     * @var string
32
     */
33
    private $environment;
34
35
    /**
36
     * @var ServiceCredentialsFactory
37
     */
38
    private $credentialsFactory;
39
40
    /**
41
     * @var string
42
     */
43
    private $serviceConfigPath;
44
45
    /**
46
     * Notification services connection config
47
     *
48
     * @var array
49
     */
50
    private $serviceConfig;
51
52
    /**
53
     * @param ServiceCredentialsFactory $credentialsFactory
54
     */
55
    public function __construct(ServiceCredentialsFactory $credentialsFactory)
56
    {
57
        $this->setEnvironment(self::ENVIRONMENT_PRODUCTION);
58
59
        $this->credentialsFactory = $credentialsFactory;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param bool $isDevelopment
66
     * @return $this
67
     */
68
    public function setDevelopmentMode($isDevelopment)
69
    {
70
        $this->environment = (bool) $isDevelopment ?
71
            self::ENVIRONMENT_DEVELOPMENT :
72
            self::ENVIRONMENT_PRODUCTION;
73
        return $this;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getEnvironment()
80
    {
81
        return $this->environment;
82
    }
83
84
    /**
85
     * @param string $environment
86
     * @return $this
87
     */
88
    public function setEnvironment($environment)
89
    {
90
        $this->environment = $environment;
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $serviceConfigPath
96
     * @return $this
97
     */
98
    public function setServiceConfigPath($serviceConfigPath)
99
    {
100
        $this->serviceConfigPath = $serviceConfigPath;
101
        return $this;
102
    }
103
104
    /**
105
     * @return $this
106
     */
107
    public function setDefaultConfigPath()
108
    {
109
        $path =
110
            __DIR__
111
            . DIRECTORY_SEPARATOR . '..'
112
            . DIRECTORY_SEPARATOR . 'Resources'
113
            . DIRECTORY_SEPARATOR . 'notification_services.json';
114
115
        $this->setServiceConfigPath($path);
116
        return $this;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getServiceConfig()
123
    {
124
        if (!$this->serviceConfig) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->serviceConfig 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...
125
            $this->loadServicesConfig();
126
        }
127
128
        return $this->serviceConfig;
129
    }
130
131
    /**
132
     * Gets notification service url by service name
133
     *
134
     * @param string $serviceName
135
     * @param bool $isFeedback
136
     * @return array
137
     */
138
    public function getServiceURL($serviceName, $isFeedback = false)
139
    {
140
        $serviceName = NotificationServices::validateServiceName($serviceName);
141
        $serviceType = $isFeedback ? self::FEEDBACK_SERVICE : self::PUSH_SERVICE;
142
        $environment = $this->getEnvironment();
143
144
        $serviceConfig = $this->getServiceConfig();
145
146
        if (empty($serviceConfig[$serviceName][$serviceType][static::ENVIRONMENT_DEVELOPMENT])) {
147
            $environment = static::ENVIRONMENT_PRODUCTION;
148
        }
149
150
        if (empty($serviceConfig[$serviceName][$serviceType][$environment])) {
151
            throw new DomainException("Service url is not defined");
152
        }
153
154
        return $serviceConfig[$serviceName][$serviceType][$environment];
155
    }
156
157
    /**
158
     * Creates client server connection by service name and sender credentials
159
     *
160
     * @param string $serviceName
161
     * @param bool $isFeedback
162
     * @return ServiceClientInterface
163
     */
164
    public function createServiceClient($serviceName, $isFeedback = false)
165
    {
166
        $serviceUrl = $this->getServiceURL($serviceName, $isFeedback);
167
168
        $clientClass = sprintf(
169
            'Zbox\UnifiedPush\NotificationService\%s\%s',
170
            $serviceName,
171
            $isFeedback ? 'ServiceFeedbackClient' : 'ServiceClient'
172
        );
173
174
        $credentials        = $this->credentialsFactory->getCredentialsByService($serviceName);
175
        $clientConnection   = new $clientClass($serviceUrl, $credentials);
176
177
        return $clientConnection;
178
    }
179
180
    /**
181
     * Load notification services connection data
182
     *
183
     * @return $this
184
     */
185
    public function loadServicesConfig()
186
    {
187
        $configPath = $this->serviceConfigPath;
188
189
        if (!file_exists($configPath)) {
190
            throw new InvalidArgumentException(
191
                sprintf(
192
                    "Service config file '%s' doesn`t exists",
193
                    $configPath
194
                )
195
            );
196
        }
197
198
        $config = json_decode(file_get_contents($configPath), true);
199
200
        if (empty($config)) {
201
            throw new RuntimeException('Empty credentials config');
202
        }
203
204
        $this->serviceConfig = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type * is incompatible with the declared type array of property $serviceConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
205
206
        return $this;
207
    }
208
}
209