1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Plugins |
7
|
|
|
* @package Payever\Plugins |
8
|
|
|
* @author payever GmbH <[email protected]> |
9
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
10
|
|
|
* @copyright 2017-2021 payever GmbH |
11
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
12
|
|
|
* @link https://docs.payever.org/shopsystems/api/getting-started |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Payever\ExternalIntegration\Plugins; |
16
|
|
|
|
17
|
|
|
use Payever\ExternalIntegration\Core\Authorization\OauthTokenList; |
18
|
|
|
use Payever\ExternalIntegration\Core\Base\ClientConfigurationInterface; |
19
|
|
|
use Payever\ExternalIntegration\Core\Base\HttpClientInterface; |
20
|
|
|
use Payever\ExternalIntegration\Core\CommonApiClient; |
21
|
|
|
use Payever\ExternalIntegration\Core\Http\RequestBuilder; |
22
|
|
|
use Payever\ExternalIntegration\Core\Http\RequestEntity; |
23
|
|
|
use Payever\ExternalIntegration\Core\Http\Response; |
24
|
|
|
use Payever\ExternalIntegration\Core\Http\ResponseEntity; |
25
|
|
|
use Payever\ExternalIntegration\Plugins\Base\PluginRegistryInfoProviderInterface; |
26
|
|
|
use Payever\ExternalIntegration\Plugins\Base\PluginsApiClientInterface; |
27
|
|
|
use Payever\ExternalIntegration\Plugins\Http\RequestEntity\PluginRegistryRequestEntity; |
28
|
|
|
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\CommandsResponseEntity; |
29
|
|
|
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\PluginRegistryResponseEntity; |
30
|
|
|
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\PluginVersionResponseEntity; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
34
|
|
|
*/ |
35
|
|
|
class PluginsApiClient extends CommonApiClient implements PluginsApiClientInterface |
36
|
|
|
{ |
37
|
|
|
const SUB_URL_REGISTER = 'api/plugin/registry/register'; |
38
|
|
|
const SUB_URL_UNREGISTER = 'api/plugin/registry/unregister'; |
39
|
|
|
const SUB_URL_ACK_COMMAND = 'api/plugin/registry/ack/%s'; |
40
|
|
|
const SUB_URL_GET_COMMANDS = 'api/plugin/command/list'; |
41
|
|
|
const SUB_URL_GET_LATEST_VERSION = 'api/plugin/channel/%s/latest?cmsVersion=%s'; |
42
|
|
|
|
43
|
|
|
/** @var PluginRegistryInfoProviderInterface */ |
44
|
|
|
private $registryInfoProvider; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param PluginRegistryInfoProviderInterface $registryInfoProvider |
48
|
|
|
* @param ClientConfigurationInterface $clientConfiguration |
49
|
|
|
* @param OauthTokenList|null $oauthTokenList |
50
|
|
|
* @param HttpClientInterface|null $httpClient |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
PluginRegistryInfoProviderInterface $registryInfoProvider, |
55
|
|
|
ClientConfigurationInterface $clientConfiguration, |
56
|
|
|
OauthTokenList $oauthTokenList = null, |
57
|
|
|
HttpClientInterface $httpClient = null |
58
|
|
|
) { |
59
|
|
|
parent::__construct($clientConfiguration, $oauthTokenList, $httpClient); |
60
|
|
|
|
61
|
|
|
$this->registryInfoProvider = $registryInfoProvider; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function getRegistryInfoProvider() |
68
|
|
|
{ |
69
|
|
|
return $this->registryInfoProvider; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
* |
75
|
|
|
* @throws \Exception |
76
|
|
|
*/ |
77
|
|
|
public function registerPlugin() |
78
|
|
|
{ |
79
|
|
|
$url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_REGISTER); |
80
|
|
|
|
81
|
|
|
return $this->doPublicJsonPostRequest( |
82
|
|
|
$url, |
83
|
|
|
$this->buildRegistryRequestEntity(true), |
84
|
|
|
new PluginRegistryResponseEntity() |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
* |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function unregisterPlugin() |
94
|
|
|
{ |
95
|
|
|
$url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_UNREGISTER); |
96
|
|
|
|
97
|
|
|
return $this->doPublicJsonPostRequest( |
98
|
|
|
$url, |
99
|
|
|
$this->buildRegistryRequestEntity(), |
100
|
|
|
new PluginRegistryResponseEntity() |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function acknowledgePluginCommand($commandId) |
110
|
|
|
{ |
111
|
|
|
return $this->doPublicJsonPostRequest( |
112
|
|
|
$this->buildAcknowledgePluginCommandUrl($commandId), |
113
|
|
|
$this->buildRegistryRequestEntity(), |
114
|
|
|
new PluginRegistryResponseEntity() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
* |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
|
|
public function getCommands($fromTimestamp = null) |
124
|
|
|
{ |
125
|
|
|
$request = RequestBuilder::get($this->buildGetCommandsUrl($fromTimestamp)) |
126
|
|
|
->setResponseEntity(new CommandsResponseEntity()) |
127
|
|
|
->build(); |
128
|
|
|
|
129
|
|
|
return $this->getHttpClient()->execute($request); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
* |
135
|
|
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
|
|
public function getLatestPluginVersion() |
138
|
|
|
{ |
139
|
|
|
$infoProvider = $this->getRegistryInfoProvider(); |
140
|
|
|
|
141
|
|
|
$path = sprintf( |
142
|
|
|
static::SUB_URL_GET_LATEST_VERSION, |
143
|
|
|
$this->configuration->getChannelSet(), |
144
|
|
|
$infoProvider->getCmsVersion() |
145
|
|
|
); |
146
|
|
|
$url = sprintf('%s%s', $this->getLiveBaseUrl(), $path); |
147
|
|
|
|
148
|
|
|
$request = RequestBuilder::get($url) |
149
|
|
|
->setResponseEntity(new PluginVersionResponseEntity()) |
150
|
|
|
->build(); |
151
|
|
|
|
152
|
|
|
return $this->getHttpClient()->execute($request); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param bool $extended whether we should build entity for Register request |
157
|
|
|
* |
158
|
|
|
* @return PluginRegistryRequestEntity |
159
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
160
|
|
|
*/ |
161
|
|
|
private function buildRegistryRequestEntity($extended = false) |
162
|
|
|
{ |
163
|
|
|
$requestEntity = new PluginRegistryRequestEntity(); |
164
|
|
|
$requestEntity |
165
|
|
|
->setChannel($this->registryInfoProvider->getChannel()) |
166
|
|
|
->setHost($this->registryInfoProvider->getHost()) |
167
|
|
|
; |
168
|
|
|
|
169
|
|
|
if ($extended) { |
170
|
|
|
$requestEntity |
171
|
|
|
->setPluginVersion($this->registryInfoProvider->getPluginVersion()) |
172
|
|
|
->setCmsVersion($this->registryInfoProvider->getCmsVersion()) |
173
|
|
|
->setSupportedCommands($this->registryInfoProvider->getSupportedCommands()) |
174
|
|
|
->setCommandEndpoint($this->registryInfoProvider->getCommandEndpoint()) |
175
|
|
|
->setBusinessIds($this->registryInfoProvider->getBusinessIds()) |
176
|
|
|
; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $requestEntity; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $url |
184
|
|
|
* @param RequestEntity $requestEntity |
185
|
|
|
* @param ResponseEntity $responseEntity |
186
|
|
|
* @return Response |
187
|
|
|
* |
188
|
|
|
* @throws \Exception |
189
|
|
|
*/ |
190
|
|
|
private function doPublicJsonPostRequest($url, RequestEntity $requestEntity, ResponseEntity $responseEntity) |
191
|
|
|
{ |
192
|
|
|
$request = RequestBuilder::post($url) |
193
|
|
|
->contentTypeIsJson() |
194
|
|
|
->setRequestEntity($requestEntity) |
195
|
|
|
->setResponseEntity($responseEntity) |
196
|
|
|
->build(); |
197
|
|
|
|
198
|
|
|
return $this->getHttpClient()->execute($request); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $commandId |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
private function buildAcknowledgePluginCommandUrl($commandId) |
206
|
|
|
{ |
207
|
|
|
return sprintf('%s%s', $this->getLiveBaseUrl(), sprintf(static::SUB_URL_ACK_COMMAND, $commandId)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param int|null $fromTimestamp |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
private function buildGetCommandsUrl($fromTimestamp = null) |
215
|
|
|
{ |
216
|
|
|
$infoProvider = $this->getRegistryInfoProvider(); |
217
|
|
|
|
218
|
|
|
$url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_GET_COMMANDS); |
219
|
|
|
$url .= sprintf( |
220
|
|
|
'?channelType=%s&cmsVersion=%s&pluginVersion=%s', |
221
|
|
|
$this->getConfiguration()->getChannelSet(), |
222
|
|
|
$infoProvider->getCmsVersion(), |
223
|
|
|
$infoProvider->getPluginVersion() |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
if ((int) $fromTimestamp > 0) { |
227
|
|
|
$url .= sprintf('&from=%s', $fromTimestamp); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $url; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
private function getLiveBaseUrl() |
237
|
|
|
{ |
238
|
|
|
$url = $this->configuration->getCustomLiveUrl() ?: static::URL_LIVE; |
239
|
|
|
|
240
|
|
|
if (substr($url, -1) != '/') { |
241
|
|
|
$url .= '/'; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $url; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|