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 ( 31963b...74f330 )
by Alexander
02:26
created

ServiceClientFactory::getServiceConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
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\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 array
106
     */
107
    public function getServiceConfig()
108
    {
109
        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...
110
            $this->loadServicesConfig();
111
        }
112
113
        return $this->serviceConfig;
114
    }
115
116
    /**
117
     * Gets notification service url by service name
118
     *
119
     * @param string $serviceName
120
     * @param bool $isFeedback
121
     * @return array
122
     */
123
    public function getServiceURL($serviceName, $isFeedback = false)
124
    {
125
        $serviceName = NotificationServices::validateServiceName($serviceName);
126
        $serviceType = $isFeedback ? self::FEEDBACK_SERVICE : self::PUSH_SERVICE;
127
        $environment = $this->getEnvironment();
128
129
        $serviceConfig = $this->getServiceConfig();
130
131
        if (empty($serviceConfig[$serviceName][$serviceType][static::ENVIRONMENT_DEVELOPMENT])) {
132
            $environment = static::ENVIRONMENT_PRODUCTION;
133
        }
134
135
        if (empty($serviceConfig[$serviceName][$serviceType][$environment])) {
136
            throw new DomainException("Service url is not defined");
137
        }
138
139
        return $serviceConfig[$serviceName][$serviceType][$environment];
140
    }
141
142
    /**
143
     * Creates client server connection by service name and sender credentials
144
     *
145
     * @param string $serviceName
146
     * @param bool $isFeedback
147
     * @return ServiceClientInterface
148
     */
149
    public function createServiceClient($serviceName, $isFeedback = false)
150
    {
151
        $serviceUrl = $this->getServiceURL($serviceName, $isFeedback);
152
153
        $clientClass = sprintf(
154
            'Zbox\UnifiedPush\NotificationService\%s\%s',
155
            $serviceName,
156
            $isFeedback ? 'ServiceFeedbackClient' : 'ServiceClient'
157
        );
158
159
        $credentials        = $this->credentialsFactory->getCredentialsByService($serviceName);
160
        $clientConnection   = new $clientClass($serviceUrl, $credentials);
161
162
        return $clientConnection;
163
    }
164
165
    /**
166
     * Load notification services connection data
167
     *
168
     * @return $this
169
     */
170
    public function loadServicesConfig()
171
    {
172
        $configPath = $this->serviceConfigPath;
173
174
        if (!file_exists($configPath)) {
175
            throw new InvalidArgumentException(
176
                sprintf(
177
                    "Service config file '%s' doesn`t exists",
178
                    $configPath
179
                )
180
            );
181
        }
182
183
        $config = json_decode(file_get_contents($configPath), true);
184
185
        if (empty($config)) {
186
            throw new RuntimeException('Empty credentials config');
187
        }
188
189
        $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...
190
191
        return $this;
192
    }
193
}
194