1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 5.4 and 7 |
4
|
|
|
* |
5
|
|
|
* @package Payever\Inventory |
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\Inventory; |
12
|
|
|
|
13
|
|
|
use Payever\ExternalIntegration\Core\Base\OauthTokenInterface; |
14
|
|
|
use Payever\ExternalIntegration\Core\Http\RequestBuilder; |
15
|
|
|
use Payever\ExternalIntegration\Core\Http\ResponseEntity\DynamicResponse; |
16
|
|
|
use Payever\ExternalIntegration\Inventory\Base\InventoryApiClientInterface; |
17
|
|
|
use Payever\ExternalIntegration\Inventory\Base\InventoryIteratorInterface; |
18
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity; |
19
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity; |
20
|
|
|
use Payever\ExternalIntegration\ThirdParty\ThirdPartyApiClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* PHP version 5.4 and 7 |
24
|
|
|
* |
25
|
|
|
* @package Payever\Inventory |
26
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
27
|
|
|
* @copyright 2017-2019 payever GmbH |
28
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
29
|
|
|
*/ |
30
|
|
|
class InventoryApiClient extends ThirdPartyApiClient implements InventoryApiClientInterface |
31
|
|
|
{ |
32
|
|
|
const SUB_URL_INVENTORY_CREATE = 'api/inventory/%s'; |
33
|
|
|
const SUB_URL_INVENTORY_ADD = 'api/inventory/%s/add'; |
34
|
|
|
const SUB_URL_INVENTORY_SUBTRACT = 'api/inventory/%s/subtract'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
* |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function createInventory(InventoryCreateRequestEntity $entity) |
42
|
|
|
{ |
43
|
|
|
$this->configuration->assertLoaded(); |
44
|
|
|
|
45
|
|
|
$url = $this->getCreateInventoryUrl($entity->getExternalId()); |
46
|
|
|
|
47
|
|
|
$request = RequestBuilder::post($url) |
48
|
|
|
->contentTypeIsJson() |
49
|
|
|
->addRawHeader( |
50
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
51
|
|
|
) |
52
|
|
|
->setRequestEntity($entity) |
53
|
|
|
->setResponseEntity(new DynamicResponse()) |
54
|
|
|
->build() |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
return $this->getHttpClient()->execute($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
public function addInventory(InventoryChangedRequestEntity $entity) |
66
|
|
|
{ |
67
|
|
|
$this->configuration->assertLoaded(); |
68
|
|
|
|
69
|
|
|
$url = $this->getAddInventoryUrl($entity->getExternalId()); |
70
|
|
|
|
71
|
|
|
$request = RequestBuilder::post($url) |
72
|
|
|
->contentTypeIsJson() |
73
|
|
|
->addRawHeader( |
74
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
75
|
|
|
) |
76
|
|
|
->setRequestEntity($entity) |
77
|
|
|
->setResponseEntity(new DynamicResponse()) |
78
|
|
|
->build() |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
return $this->getHttpClient()->execute($request); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param InventoryChangedRequestEntity $entity |
86
|
|
|
* @return \Payever\ExternalIntegration\Core\Http\Response |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function subtractInventory(InventoryChangedRequestEntity $entity) |
90
|
|
|
{ |
91
|
|
|
$this->configuration->assertLoaded(); |
92
|
|
|
|
93
|
|
|
$url = $this->getSubtractInventoryUrl($entity->getExternalId()); |
94
|
|
|
|
95
|
|
|
$request = RequestBuilder::post($url) |
96
|
|
|
->contentTypeIsJson() |
97
|
|
|
->addRawHeader( |
98
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
99
|
|
|
) |
100
|
|
|
->setRequestEntity($entity) |
101
|
|
|
->setResponseEntity(new DynamicResponse()) |
102
|
|
|
->build() |
103
|
|
|
; |
104
|
|
|
|
105
|
|
|
return $this->getHttpClient()->execute($request); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
|
public function exportInventory(InventoryIteratorInterface $inventoryIterator, $externalId) |
112
|
|
|
{ |
113
|
|
|
$this->configuration->assertLoaded(); |
114
|
|
|
|
115
|
|
|
$successCount = 0; |
116
|
|
|
|
117
|
|
|
foreach ($inventoryIterator as $inventoryCreatedRequestEntity) { |
118
|
|
|
try { |
119
|
|
|
$inventoryCreatedRequestEntity->setExternalId($externalId); |
120
|
|
|
$this->createInventory($inventoryCreatedRequestEntity); |
121
|
|
|
} catch (\Exception $exception) { |
122
|
|
|
$this->configuration->getLogger() |
123
|
|
|
->critical( |
124
|
|
|
sprintf( |
125
|
|
|
'Inventory item SDU=%s failed to export: %s', |
126
|
|
|
$inventoryCreatedRequestEntity->getSku(), |
127
|
|
|
$exception->getMessage() |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $successCount; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $externalId |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
private function getCreateInventoryUrl($externalId) |
142
|
|
|
{ |
143
|
|
|
return $this->getThirdPartyBaseUrl() |
144
|
|
|
. sprintf(static::SUB_URL_INVENTORY_CREATE, $externalId) |
145
|
|
|
; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $externalId |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
private function getAddInventoryUrl($externalId) |
153
|
|
|
{ |
154
|
|
|
return $this->getThirdPartyBaseUrl() |
155
|
|
|
. sprintf(static::SUB_URL_INVENTORY_ADD, $externalId) |
156
|
|
|
; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $externalId |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
private function getSubtractInventoryUrl($externalId) |
164
|
|
|
{ |
165
|
|
|
return $this->getThirdPartyBaseUrl() |
166
|
|
|
. sprintf(static::SUB_URL_INVENTORY_SUBTRACT, $externalId) |
167
|
|
|
; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|