1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 7 |
5
|
|
|
* |
6
|
|
|
* @category ThirdParty |
7
|
|
|
* @package Payever\ThirdParty |
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\ThirdParty; |
16
|
|
|
|
17
|
|
|
use Payever\ExternalIntegration\Core\CommonProductsThirdPartyApiClient; |
18
|
|
|
use Payever\ExternalIntegration\Core\Http\RequestBuilder; |
19
|
|
|
use Payever\ExternalIntegration\Core\Http\ResponseEntity\DynamicResponse; |
20
|
|
|
use Payever\ExternalIntegration\ThirdParty\Base\ThirdPartyApiClientInterface; |
21
|
|
|
use Payever\ExternalIntegration\ThirdParty\Http\RequestEntity\SubscriptionRequestEntity; |
22
|
|
|
use Payever\ExternalIntegration\ThirdParty\Http\ResponseEntity\BusinessResponseEntity; |
23
|
|
|
use Payever\ExternalIntegration\ThirdParty\Http\ResponseEntity\SubscriptionResponseEntity; |
24
|
|
|
|
25
|
|
|
class ThirdPartyApiClient extends CommonProductsThirdPartyApiClient implements ThirdPartyApiClientInterface |
26
|
|
|
{ |
27
|
|
|
const SUB_URL_BUSINESS_INFO = 'api/business/%s/plugins'; |
28
|
|
|
const SUB_URL_CONNECTION = 'api/business/%s/connection/authorization/%s'; |
29
|
|
|
const SUB_URL_INTEGRATION = 'api/business/%s/integration/%s'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
* |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public function getBusinessRequest() |
37
|
|
|
{ |
38
|
|
|
$this->configuration->assertLoaded(); |
39
|
|
|
|
40
|
|
|
$request = RequestBuilder::get($this->getBusinessInfoUrl($this->configuration->getBusinessUuid())) |
41
|
|
|
->addRawHeader( |
42
|
|
|
$this->getToken()->getAuthorizationString() |
43
|
|
|
) |
44
|
|
|
->setResponseEntity(new BusinessResponseEntity()) |
45
|
|
|
->build(); |
46
|
|
|
|
47
|
|
|
return $this->getHttpClient()->execute($request); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
* |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function getSubscriptionStatus(SubscriptionRequestEntity $requestEntity) |
56
|
|
|
{ |
57
|
|
|
$this->configuration->assertLoaded(); |
58
|
|
|
|
59
|
|
|
$this->fillSubscriptionEntityFromConfiguration($requestEntity); |
60
|
|
|
|
61
|
|
|
$request = RequestBuilder::get($this->getConnectionUrl($requestEntity)) |
62
|
|
|
->addRawHeader( |
63
|
|
|
$this->getToken()->getAuthorizationString() |
64
|
|
|
) |
65
|
|
|
->setResponseEntity(new SubscriptionResponseEntity()) |
66
|
|
|
->build(); |
67
|
|
|
|
68
|
|
|
return $this->getHttpClient()->execute($request); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
* |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
|
|
public function subscribe(SubscriptionRequestEntity $requestEntity) |
77
|
|
|
{ |
78
|
|
|
$this->configuration->assertLoaded(); |
79
|
|
|
|
80
|
|
|
$this->fillSubscriptionEntityFromConfiguration($requestEntity); |
81
|
|
|
|
82
|
|
|
$request = RequestBuilder::post($this->getIntegrationUrl($requestEntity)) |
83
|
|
|
->contentTypeIsJson() |
84
|
|
|
->addRawHeader( |
85
|
|
|
$this->getToken()->getAuthorizationString() |
86
|
|
|
) |
87
|
|
|
->setRequestEntity($requestEntity) |
88
|
|
|
->setResponseEntity(new SubscriptionResponseEntity()) |
89
|
|
|
->build(); |
90
|
|
|
|
91
|
|
|
return $this->getHttpClient()->execute($request); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
* |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function unsubscribe(SubscriptionRequestEntity $requestEntity) |
100
|
|
|
{ |
101
|
|
|
$this->configuration->assertLoaded(); |
102
|
|
|
|
103
|
|
|
$this->fillSubscriptionEntityFromConfiguration($requestEntity); |
104
|
|
|
|
105
|
|
|
$request = RequestBuilder::delete($this->getConnectionUrl($requestEntity)) |
106
|
|
|
->addRawHeader( |
107
|
|
|
$this->getToken()->getAuthorizationString() |
108
|
|
|
) |
109
|
|
|
->setResponseEntity(new DynamicResponse()) |
110
|
|
|
->build(); |
111
|
|
|
|
112
|
|
|
return $this->getHttpClient()->execute($request); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $businessUuid |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function getBusinessInfoUrl($businessUuid) |
121
|
|
|
{ |
122
|
|
|
return $this->getBaseUrl() . sprintf(static::SUB_URL_BUSINESS_INFO, $businessUuid); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param SubscriptionRequestEntity $requestEntity |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
protected function getConnectionUrl(SubscriptionRequestEntity $requestEntity) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$path = sprintf( |
132
|
|
|
static::SUB_URL_CONNECTION, |
133
|
|
|
$requestEntity->getBusinessUuid(), |
134
|
|
|
$requestEntity->getExternalId() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $this->getBaseUrl() . $path; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param SubscriptionRequestEntity $requestEntity |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
protected function getIntegrationUrl(SubscriptionRequestEntity $requestEntity) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$path = sprintf( |
147
|
|
|
static::SUB_URL_INTEGRATION, |
148
|
|
|
$requestEntity->getBusinessUuid(), |
149
|
|
|
$requestEntity->getThirdPartyName() |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return $this->getBaseUrl() . $path; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param SubscriptionRequestEntity $requestEntity |
157
|
|
|
*/ |
158
|
|
|
private function fillSubscriptionEntityFromConfiguration(SubscriptionRequestEntity $requestEntity) |
159
|
|
|
{ |
160
|
|
|
if (!$requestEntity->getBusinessUuid()) { |
161
|
|
|
$requestEntity->setBusinessUuid($this->configuration->getBusinessUuid()); |
162
|
|
|
} |
163
|
|
|
if (!$requestEntity->getThirdPartyName()) { |
164
|
|
|
$requestEntity->setThirdPartyName($this->configuration->getChannelSet()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.