Passed
Push — master ( afb8f6...f8ce69 )
by payever
03:32
created

PluginsApiClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 66
c 1
b 0
f 0
dl 0
loc 195
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 7 1
A doPublicJsonPostRequest() 0 9 1
A getLatestPluginVersion() 0 16 1
A acknowledgePluginCommand() 0 6 1
A unregisterPlugin() 0 8 1
A buildGetCommandsUrl() 0 10 2
A getLiveBaseUrl() 0 9 3
A buildRegistryRequestEntity() 0 19 2
A getRegistryInfoProvider() 0 3 1
A registerPlugin() 0 8 1
A __construct() 0 9 1
A buildAcknowledgePluginCommandUrl() 0 3 1
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
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\PluginVersionResponseEntity;
27
28
class PluginsApiClient extends CommonApiClient implements PluginsApiClientInterface
29
{
30
    const SUB_URL_REGISTER = 'api/plugin/registry/register';
31
    const SUB_URL_UNREGISTER = 'api/plugin/registry/unregister';
32
    const SUB_URL_ACK_COMMAND = 'api/plugin/registry/ack/%s';
33
    const SUB_URL_GET_COMMANDS = 'api/plugin/command/list';
34
    const SUB_URL_GET_LATEST_VERSION = 'api/plugin/channel/%s/latest?cmsVersion=%s';
35
36
    /** @var PluginRegistryInfoProviderInterface */
37
    private $registryInfoProvider;
38
39
    public function __construct(
40
        PluginRegistryInfoProviderInterface $registryInfoProvider,
41
        ClientConfigurationInterface $clientConfiguration,
42
        OauthTokenList $oauthTokenList = null,
43
        HttpClientInterface $httpClient = null
44
    ) {
45
        parent::__construct($clientConfiguration, $oauthTokenList, $httpClient);
46
47
        $this->registryInfoProvider = $registryInfoProvider;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getRegistryInfoProvider()
54
    {
55
        return $this->registryInfoProvider;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     *
61
     * @throws \Exception
62
     */
63
    public function registerPlugin()
64
    {
65
        $url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_REGISTER);
66
67
        return $this->doPublicJsonPostRequest(
68
            $url,
69
            $this->buildRegistryRequestEntity(true),
70
            new PluginRegistryResponseEntity()
71
        );
72
    }
73
74
    /**
75
     * @inheritdoc
76
     *
77
     * @throws \Exception
78
     */
79
    public function unregisterPlugin()
80
    {
81
        $url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_UNREGISTER);
82
83
        return $this->doPublicJsonPostRequest(
84
            $url,
85
            $this->buildRegistryRequestEntity(),
86
            new PluginRegistryResponseEntity()
87
        );
88
    }
89
90
    /**
91
     * @inheritdoc
92
     *
93
     * @throws \Exception
94
     */
95
    public function acknowledgePluginCommand($commandId)
96
    {
97
        return $this->doPublicJsonPostRequest(
98
            $this->buildAcknowledgePluginCommandUrl($commandId),
99
            $this->buildRegistryRequestEntity(),
100
            new PluginRegistryResponseEntity()
101
        );
102
    }
103
104
    /**
105
     * @inheritdoc
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
     * @inheritDoc
120
     *
121
     * @throws \Exception
122
     */
123
    public function getLatestPluginVersion()
124
    {
125
        $infoProvider = $this->getRegistryInfoProvider();
126
127
        $path = sprintf(
128
            static::SUB_URL_GET_LATEST_VERSION,
129
            $this->configuration->getChannelSet(),
130
            $infoProvider->getCmsVersion()
131
        );
132
        $url = sprintf('%s%s', $this->getLiveBaseUrl(), $path);
133
134
        $request = RequestBuilder::get($url)
135
            ->setResponseEntity(new PluginVersionResponseEntity())
136
            ->build();
137
138
        return $this->getHttpClient()->execute($request);
139
    }
140
141
    /**
142
     * @param bool $extended whether we should build entity for Register request
143
     *
144
     * @return PluginRegistryRequestEntity
145
     */
146
    private function buildRegistryRequestEntity($extended = false)
147
    {
148
        $requestEntity = new PluginRegistryRequestEntity();
149
        $requestEntity
150
            ->setChannel($this->registryInfoProvider->getChannel())
151
            ->setHost($this->registryInfoProvider->getHost())
152
        ;
153
154
        if ($extended) {
155
            $requestEntity
156
                ->setPluginVersion($this->registryInfoProvider->getPluginVersion())
157
                ->setCmsVersion($this->registryInfoProvider->getCmsVersion())
158
                ->setSupportedCommands($this->registryInfoProvider->getSupportedCommands())
159
                ->setCommandEndpoint($this->registryInfoProvider->getCommandEndpoint())
160
                ->setBusinessIds($this->registryInfoProvider->getBusinessIds())
161
            ;
162
        }
163
164
        return $requestEntity;
165
    }
166
167
    /**
168
     * @param string $url
169
     * @param RequestEntity $requestEntity
170
     * @param ResponseEntity $responseEntity
171
     * @return Response
172
     *
173
     * @throws \Exception
174
     */
175
    private function doPublicJsonPostRequest($url, RequestEntity $requestEntity, ResponseEntity $responseEntity)
176
    {
177
        $request = RequestBuilder::post($url)
178
            ->contentTypeIsJson()
179
            ->setRequestEntity($requestEntity)
180
            ->setResponseEntity($responseEntity)
181
            ->build();
182
183
        return $this->getHttpClient()->execute($request);
184
    }
185
186
    /**
187
     * @param string $commandId
188
     * @return string
189
     */
190
    private function buildAcknowledgePluginCommandUrl($commandId)
191
    {
192
        return sprintf('%s%s', $this->getLiveBaseUrl(), sprintf(static::SUB_URL_ACK_COMMAND, $commandId));
193
    }
194
195
    /**
196
     * @param int|null $fromTimestamp
197
     * @return string
198
     */
199
    private function buildGetCommandsUrl($fromTimestamp = null)
200
    {
201
        $url = sprintf('%s%s', $this->getLiveBaseUrl(), static::SUB_URL_GET_COMMANDS);
202
        $url .= sprintf('?channelType=%s', $this->getConfiguration()->getChannelSet());
203
204
        if ((int) $fromTimestamp > 0) {
205
            $url .= sprintf('&from=%s', $fromTimestamp);
206
        }
207
208
        return $url;
209
    }
210
211
    /**
212
     * @return string
213
     */
214
    private function getLiveBaseUrl()
215
    {
216
        $url = $this->configuration->getCustomLiveUrl() ?: static::URL_LIVE;
217
218
        if (substr($url, -1) != '/') {
219
            $url .= '/';
220
        }
221
222
        return $url;
223
    }
224
}
225