1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 5.4 and 7 |
4
|
|
|
* |
5
|
|
|
* @package Payever\Core |
6
|
|
|
* @author Andrey Puhovsky <[email protected]> |
7
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
8
|
|
|
* @copyright 2017-2019 payever GmbH |
9
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Payever\ExternalIntegration\Core; |
13
|
|
|
|
14
|
|
|
use Payever\ExternalIntegration\Core\Base\ClientConfigurationInterface; |
15
|
|
|
use Payever\ExternalIntegration\Core\Enum\ChannelSet; |
|
|
|
|
16
|
|
|
use Payever\ExternalIntegration\Core\Exception\ConfigurationException; |
17
|
|
|
use Payever\ExternalIntegration\Core\Logger\NullLogger; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* PHP version 5.4 and 7 |
22
|
|
|
* |
23
|
|
|
* @package Payever\Core |
24
|
|
|
* @author Andrey Puhovsky <[email protected]> |
25
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
26
|
|
|
* @copyright 2017-2019 payever GmbH |
27
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
28
|
|
|
*/ |
29
|
|
|
class ClientConfiguration implements ClientConfigurationInterface |
30
|
|
|
{ |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $apiMode = self::API_MODE_LIVE; |
33
|
|
|
|
34
|
|
|
/** @var string $clientId */ |
35
|
|
|
protected $clientId; |
36
|
|
|
|
37
|
|
|
/** @var string $clientSecret */ |
38
|
|
|
protected $clientSecret; |
39
|
|
|
|
40
|
|
|
/** @var string $customApiUrl */ |
41
|
|
|
protected $customApiUrl; |
42
|
|
|
|
43
|
|
|
/** @var string $businessUuid */ |
44
|
|
|
protected $businessUuid; |
45
|
|
|
|
46
|
|
|
/** @var string $channelSet */ |
47
|
|
|
protected $channelSet = ChannelSet::CHANNEL_OTHER_SHOPSYSTEM; |
48
|
|
|
|
49
|
|
|
/** @var LoggerInterface */ |
50
|
|
|
protected $logger; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* ClientConfiguration constructor. |
54
|
|
|
* |
55
|
|
|
* @param string|null $clientId |
56
|
|
|
* @param string|null $clientSecret |
57
|
|
|
* @param string|null $businessUuid |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
$clientId = null, |
61
|
|
|
$clientSecret = null, |
62
|
|
|
$businessUuid = null |
63
|
|
|
) { |
64
|
|
|
$this->clientId = $clientId; |
65
|
|
|
$this->clientSecret = $clientSecret; |
66
|
|
|
$this->businessUuid = $businessUuid; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getClientId() |
73
|
|
|
{ |
74
|
|
|
return $this->clientId; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getClientSecret() |
81
|
|
|
{ |
82
|
|
|
return $this->clientSecret; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getBusinessUuid() |
89
|
|
|
{ |
90
|
|
|
return $this->businessUuid; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getApiMode() |
97
|
|
|
{ |
98
|
|
|
return $this->apiMode; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getChannelSet() |
105
|
|
|
{ |
106
|
|
|
return $this->channelSet; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getCustomApiUrl() |
113
|
|
|
{ |
114
|
|
|
return $this->customApiUrl; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getHash() |
121
|
|
|
{ |
122
|
|
|
return md5($this->getClientId() . $this->getClientSecret()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return LoggerInterface |
127
|
|
|
*/ |
128
|
|
|
public function getLogger() |
129
|
|
|
{ |
130
|
|
|
if (is_null($this->logger)) { |
131
|
|
|
$this->logger = new NullLogger(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->logger; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets Client ID |
139
|
|
|
* |
140
|
|
|
* @param mixed $clientId |
141
|
|
|
* |
142
|
|
|
* @return self; |
143
|
|
|
*/ |
144
|
|
|
public function setClientId($clientId) |
145
|
|
|
{ |
146
|
|
|
$this->clientId = $clientId; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets Client Secret |
153
|
|
|
* |
154
|
|
|
* @param mixed $clientSecret |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
|
|
public function setClientSecret($clientSecret) |
159
|
|
|
{ |
160
|
|
|
$this->clientSecret = $clientSecret; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets Business UUID |
167
|
|
|
* |
168
|
|
|
* @param mixed $businessUuid |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function setBusinessUuid($businessUuid) |
173
|
|
|
{ |
174
|
|
|
$this->businessUuid = $businessUuid; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $apiMode |
181
|
|
|
* @return self |
182
|
|
|
*/ |
183
|
|
|
public function setApiMode($apiMode) |
184
|
|
|
{ |
185
|
|
|
$this->apiMode = $apiMode; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Sets Channel set |
192
|
|
|
* |
193
|
|
|
* @param mixed $channelSet |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
* |
197
|
|
|
* @throws \Exception |
198
|
|
|
*/ |
199
|
|
|
public function setChannelSet($channelSet = null) |
200
|
|
|
{ |
201
|
|
|
if (in_array($channelSet, ChannelSet::enum())) { |
202
|
|
|
$this->channelSet = $channelSet; |
203
|
|
|
} else { |
204
|
|
|
throw new ConfigurationException(sprintf('Channel Set `%s` is not valid', $channelSet)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @internal |
212
|
|
|
* |
213
|
|
|
* Sets Custom API URL for all packages at once |
214
|
|
|
* |
215
|
|
|
* @param string $customApiUrl |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
|
|
public function setCustomApiUrl($customApiUrl) |
220
|
|
|
{ |
221
|
|
|
$this->customApiUrl = $customApiUrl; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param LoggerInterface $logger |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function setLogger(LoggerInterface $logger) |
232
|
|
|
{ |
233
|
|
|
$this->logger = $logger; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @inheritdoc |
240
|
|
|
*/ |
241
|
|
|
public function isLoaded() |
242
|
|
|
{ |
243
|
|
|
return |
244
|
|
|
$this->getClientId() && |
245
|
|
|
$this->getClientSecret() && |
246
|
|
|
$this->getBusinessUuid() |
247
|
|
|
; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritdoc |
252
|
|
|
*/ |
253
|
|
|
public function assertLoaded() |
254
|
|
|
{ |
255
|
|
|
if (!$this->isLoaded()) { |
256
|
|
|
throw new ConfigurationException( |
257
|
|
|
"Payever API client credentials (client_id, client_secret, business_uuid) are not set." |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: