1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Inventory |
7
|
|
|
* @package Payever\Inventory |
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\Inventory; |
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\Inventory\Base\InventoryApiClientInterface; |
21
|
|
|
use Payever\ExternalIntegration\Inventory\Base\InventoryIteratorInterface; |
22
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity; |
23
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity; |
24
|
|
|
|
25
|
|
|
class InventoryApiClient extends CommonProductsThirdPartyApiClient implements InventoryApiClientInterface |
26
|
|
|
{ |
27
|
|
|
const SUB_URL_INVENTORY_CREATE = 'api/inventory/%s'; |
28
|
|
|
const SUB_URL_INVENTORY_ADD = 'api/inventory/%s/add'; |
29
|
|
|
const SUB_URL_INVENTORY_SUBTRACT = 'api/inventory/%s/subtract'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function createInventory(InventoryCreateRequestEntity $entity) |
35
|
|
|
{ |
36
|
|
|
$this->configuration->assertLoaded(); |
37
|
|
|
$url = $this->getCreateInventoryUrl($entity->getExternalId()); |
38
|
|
|
|
39
|
|
|
$request = RequestBuilder::post($url) |
40
|
|
|
->contentTypeIsJson() |
41
|
|
|
->addRawHeader( |
42
|
|
|
$this->getToken()->getAuthorizationString() |
43
|
|
|
) |
44
|
|
|
->setRequestEntity($entity) |
45
|
|
|
->setResponseEntity(new DynamicResponse()) |
46
|
|
|
->build(); |
47
|
|
|
|
48
|
|
|
return $this->executeRequest($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function addInventory(InventoryChangedRequestEntity $entity) |
55
|
|
|
{ |
56
|
|
|
$this->configuration->assertLoaded(); |
57
|
|
|
$url = $this->getAddInventoryUrl($entity->getExternalId()); |
58
|
|
|
|
59
|
|
|
$request = RequestBuilder::post($url) |
60
|
|
|
->contentTypeIsJson() |
61
|
|
|
->addRawHeader( |
62
|
|
|
$this->getToken()->getAuthorizationString() |
63
|
|
|
) |
64
|
|
|
->setRequestEntity($entity) |
65
|
|
|
->setResponseEntity(new DynamicResponse()) |
66
|
|
|
->build(); |
67
|
|
|
|
68
|
|
|
return $this->executeRequest($request); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
public function subtractInventory(InventoryChangedRequestEntity $entity) |
75
|
|
|
{ |
76
|
|
|
$this->configuration->assertLoaded(); |
77
|
|
|
$url = $this->getSubtractInventoryUrl($entity->getExternalId()); |
78
|
|
|
|
79
|
|
|
$request = RequestBuilder::post($url) |
80
|
|
|
->contentTypeIsJson() |
81
|
|
|
->addRawHeader( |
82
|
|
|
$this->getToken()->getAuthorizationString() |
83
|
|
|
) |
84
|
|
|
->setRequestEntity($entity) |
85
|
|
|
->setResponseEntity(new DynamicResponse()) |
86
|
|
|
->build(); |
87
|
|
|
|
88
|
|
|
return $this->executeRequest($request); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function exportInventory(InventoryIteratorInterface $inventoryIterator, $externalId) |
95
|
|
|
{ |
96
|
|
|
$this->configuration->assertLoaded(); |
97
|
|
|
$successCount = 0; |
98
|
|
|
|
99
|
|
|
foreach ($inventoryIterator as $requestEntity) { |
100
|
|
|
try { |
101
|
|
|
$requestEntity->setExternalId($externalId); |
102
|
|
|
$this->createInventory($requestEntity); |
103
|
|
|
} catch (\Exception $exception) { |
104
|
|
|
$this->configuration->getLogger() |
105
|
|
|
->critical( |
106
|
|
|
'Inventory item failed to export', |
107
|
|
|
[ |
108
|
|
|
'sku' => $requestEntity->getSku(), |
109
|
|
|
'exception' => $exception->getMessage(), |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
throw $exception; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $successCount; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $externalId |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function getCreateInventoryUrl($externalId) |
125
|
|
|
{ |
126
|
|
|
return $this->getBaseUrl() . sprintf(static::SUB_URL_INVENTORY_CREATE, $externalId); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $externalId |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private function getAddInventoryUrl($externalId) |
134
|
|
|
{ |
135
|
|
|
return $this->getBaseUrl() . sprintf(static::SUB_URL_INVENTORY_ADD, $externalId); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $externalId |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
private function getSubtractInventoryUrl($externalId) |
143
|
|
|
{ |
144
|
|
|
return $this->getBaseUrl() . sprintf(static::SUB_URL_INVENTORY_SUBTRACT, $externalId); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|