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 ( 281989...6cfff6 )
by Alexander
02:37
created

ServiceClientFactory::getEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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 (empty($this->serviceConfig)) {
125
            $this->loadServicesConfig();
126
        }
127
128
        return $this->serviceConfig;
129
    }
130
131
    /**
132
     * @param array $serviceConfig
133
     * @return $this
134
     */
135
    public function setServiceConfig(array $serviceConfig)
136
    {
137
        $this->serviceConfig = $serviceConfig;
138
        return $this;
139
    }
140
141
    /**
142
     * Gets notification service url by service name
143
     *
144
     * @param string $serviceName
145
     * @param bool $isFeedback
146
     * @return array
147
     */
148
    public function getServiceURL($serviceName, $isFeedback = false)
149
    {
150
        $serviceName = NotificationServices::validateServiceName($serviceName);
151
        $serviceType = $isFeedback ? self::FEEDBACK_SERVICE : self::PUSH_SERVICE;
152
        $environment = $this->getEnvironment();
153
154
        $serviceConfig = $this->getServiceConfig();
155
156
        if (empty($serviceConfig[$serviceName][$serviceType][static::ENVIRONMENT_DEVELOPMENT])) {
157
            $environment = static::ENVIRONMENT_PRODUCTION;
158
        }
159
160
        if (empty($serviceConfig[$serviceName][$serviceType][$environment])) {
161
            throw new DomainException("Service url is not defined");
162
        }
163
164
        return $serviceConfig[$serviceName][$serviceType][$environment];
165
    }
166
167
    /**
168
     * Creates client server connection by service name and sender credentials
169
     *
170
     * @param string $serviceName
171
     * @param bool $isFeedback
172
     * @return ServiceClientInterface
173
     */
174
    public function createServiceClient($serviceName, $isFeedback = false)
175
    {
176
        $serviceUrl = $this->getServiceURL($serviceName, $isFeedback);
177
178
        $clientClass = sprintf(
179
            'Zbox\UnifiedPush\NotificationService\%s\%s',
180
            $serviceName,
181
            $isFeedback ? 'ServiceFeedbackClient' : 'ServiceClient'
182
        );
183
184
        $credentials        = $this->credentialsFactory->getCredentialsByService($serviceName);
185
        $clientConnection   = new $clientClass($serviceUrl, $credentials);
186
187
        return $clientConnection;
188
    }
189
190
    /**
191
     * Load notification services connection data
192
     *
193
     * @return $this
194
     */
195
    public function loadServicesConfig()
196
    {
197
        $configPath = $this->serviceConfigPath;
198
199
        if (!file_exists($configPath)) {
200
            throw new InvalidArgumentException(
201
                sprintf(
202
                    "Service config file '%s' doesn`t exists",
203
                    $configPath
204
                )
205
            );
206
        }
207
208
        $config = json_decode(file_get_contents($configPath), true);
209
210
        if (!is_array($config)) {
211
            throw new RuntimeException('Empty credentials config');
212
        }
213
214
        $this->serviceConfig = $config;
215
216
        return $this;
217
    }
218
}
219