Passed
Push — master ( a8ff8f...cbaed7 )
by payever
02:47
created

PluginsApiClient::doPublicJsonPostRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 3
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\Exception\PayeverCommunicationException;
18
use Payever\ExternalIntegration\Core\Http\RequestBuilder;
19
use Payever\ExternalIntegration\Core\Http\RequestEntity;
20
use Payever\ExternalIntegration\Core\Http\Response;
21
use Payever\ExternalIntegration\Core\Http\ResponseEntity;
22
use Payever\ExternalIntegration\Plugins\Base\PluginRegistryInfoProviderInterface;
23
use Payever\ExternalIntegration\Plugins\Base\PluginsApiClientInterface;
24
use Payever\ExternalIntegration\Plugins\Http\RequestEntity\PluginRegistryRequestEntity;
25
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\CommandsResponseEntity;
26
use Payever\ExternalIntegration\Plugins\Http\ResponseEntity\PluginRegistryResponseEntity;
27
28
class PluginsApiClient extends CommonApiClient implements PluginsApiClientInterface
29
{
30
    const SUB_URL_REGISTER = 'api/plugins/registry/register';
31
    const SUB_URL_UNREGISTER = 'api/plugins/registry/unregister';
32
    const SUB_URL_ACK_COMMAND = 'api/plugins/registry/ack/%s';
33
    const SUB_URL_GET_COMMANDS = 'api/plugins/command/list';
34
35
    /** @var PluginRegistryInfoProviderInterface */
36
    private $registryInfoProvider;
37
38
    public function __construct(
39
        PluginRegistryInfoProviderInterface $registryInfoProvider,
40
        ClientConfigurationInterface $clientConfiguration,
41
        OauthTokenList $oauthTokenList = null,
42
        HttpClientInterface $httpClient = null
43
    ) {
44
        parent::__construct($clientConfiguration, $oauthTokenList, $httpClient);
45
46
        $this->registryInfoProvider = $registryInfoProvider;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     *
52
     * @throws PayeverCommunicationException
53
     */
54
    public function registerPlugin()
55
    {
56
        $url = sprintf('%s%s', $this->getBaseUrl(), static::SUB_URL_REGISTER);
57
58
        return $this->doPublicJsonPostRequest(
59
            $url,
60
            $this->buildRegistryRequestEntity(true),
61
            new PluginRegistryResponseEntity()
62
        );
63
    }
64
65
    /**
66
     * @inheritdoc
67
     *
68
     * @throws PayeverCommunicationException
69
     */
70
    public function unregisterPlugin()
71
    {
72
        $url = sprintf('%s%s', $this->getBaseUrl(), static::SUB_URL_UNREGISTER);
73
74
        return $this->doPublicJsonPostRequest(
75
            $url,
76
            $this->buildRegistryRequestEntity(),
77
            new PluginRegistryResponseEntity()
78
        );
79
    }
80
81
    /**
82
     * @inheritdoc
83
     *
84
     * @throws PayeverCommunicationException
85
     */
86
    public function acknowledgePluginCommand($commandId)
87
    {
88
        return $this->doPublicJsonPostRequest(
89
            $this->buildAcknowledgePluginCommandUrl($commandId),
90
            $this->buildRegistryRequestEntity(),
91
            new PluginRegistryResponseEntity()
92
        );
93
    }
94
95
    /**
96
     * @inheritdoc
97
     *
98
     * @pga-return Response<CommandsResponseEntity>
99
     *
100
     * @throws PayeverCommunicationException
101
     */
102
    public function getCommands($fromTimestamp = null)
103
    {
104
        $request = RequestBuilder::get($this->buildGetCommandsUrl($fromTimestamp))
105
            ->setResponseEntity(new CommandsResponseEntity())
106
            ->build();
107
108
        return $this->httpClient->execute($request);
109
    }
110
111
    /**
112
     * @param bool $extended whether we should build entity for Register request
113
     *
114
     * @return PluginRegistryRequestEntity
115
     */
116
    private function buildRegistryRequestEntity($extended = false)
117
    {
118
        $requestEntity = new PluginRegistryRequestEntity();
119
        $requestEntity
120
            ->setChannel($this->registryInfoProvider->getChannel())
121
            ->setHost($this->registryInfoProvider->getHost())
122
        ;
123
124
        if ($extended) {
125
            $requestEntity
126
                ->setPluginVersion($this->registryInfoProvider->getPluginVersion())
127
                ->setCmsVersion($this->registryInfoProvider->getCmsVersion())
128
                ->setSupportedCommands($this->registryInfoProvider->getSupportedCommands())
129
                ->setCommandEndpoint($this->registryInfoProvider->getCommandEndpoint())
130
                ->setBusinessIds($this->registryInfoProvider->getBusinessIds())
131
            ;
132
        }
133
134
        return $requestEntity;
135
    }
136
137
    /**
138
     * @param string $url
139
     * @param RequestEntity $requestEntity
140
     * @param ResponseEntity $responseEntity
141
     * @return Response
142
     *
143
     * @throws PayeverCommunicationException
144
     */
145
    private function doPublicJsonPostRequest($url, RequestEntity $requestEntity, ResponseEntity $responseEntity)
146
    {
147
        $request = RequestBuilder::post($url)
148
            ->contentTypeIsJson()
149
            ->setRequestEntity($requestEntity)
150
            ->setResponseEntity($responseEntity)
151
            ->build();
152
153
        return $this->httpClient->execute($request);
154
    }
155
156
    /**
157
     * @param string $commandId
158
     * @return string
159
     */
160
    private function buildAcknowledgePluginCommandUrl($commandId)
161
    {
162
        return sprintf('%s%s', $this->getBaseUrl(), sprintf(static::SUB_URL_ACK_COMMAND, $commandId));
163
    }
164
165
    /**
166
     * @param int|null $fromTimestamp
167
     * @return string
168
     */
169
    private function buildGetCommandsUrl($fromTimestamp = null)
170
    {
171
        $url = sprintf('%s%s', $this->getBaseUrl(), static::SUB_URL_GET_COMMANDS);
172
173
        if ($fromTimestamp) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fromTimestamp of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
174
            $url .= sprintf('?timestamp=%s', $fromTimestamp);
175
        }
176
177
        return $url;
178
    }
179
}
180