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