1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 5.4 and 7 |
4
|
|
|
* |
5
|
|
|
* @package Payever\Products |
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\Products; |
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\Products\Base\ProductsApiClientInterface; |
17
|
|
|
use Payever\ExternalIntegration\Products\Base\ProductsIteratorInterface; |
18
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity; |
19
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity; |
20
|
|
|
use Payever\ExternalIntegration\ThirdParty\ThirdPartyApiClient; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* PHP version 5.4 and 7 |
24
|
|
|
* |
25
|
|
|
* @package Payever\Products |
26
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
27
|
|
|
* @copyright 2017-2019 payever GmbH |
28
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
29
|
|
|
*/ |
30
|
|
|
class ProductsApiClient extends ThirdPartyApiClient implements ProductsApiClientInterface |
31
|
|
|
{ |
32
|
|
|
const SUB_URL_PRODUCT = 'api/product/%s'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
* |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function createProduct(ProductRequestEntity $entity) |
40
|
|
|
{ |
41
|
|
|
$this->configuration->assertLoaded(); |
42
|
|
|
|
43
|
|
|
$url = $this->getProductUrl($entity->getExternalId()); |
44
|
|
|
|
45
|
|
|
$request = RequestBuilder::post($url) |
46
|
|
|
->contentTypeIsJson() |
47
|
|
|
->addRawHeader( |
48
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
49
|
|
|
) |
50
|
|
|
->setRequestEntity($entity) |
51
|
|
|
->setResponseEntity(new DynamicResponse()) |
52
|
|
|
->build() |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
return $this->getHttpClient()->execute($request); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
* |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
public function updateProduct(ProductRequestEntity $entity) |
64
|
|
|
{ |
65
|
|
|
$this->configuration->assertLoaded(); |
66
|
|
|
|
67
|
|
|
$url = $this->getProductUrl($entity->getExternalId()); |
68
|
|
|
|
69
|
|
|
$request = RequestBuilder::patch($url) |
70
|
|
|
->contentTypeIsJson() |
71
|
|
|
->addRawHeader( |
72
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
73
|
|
|
) |
74
|
|
|
->setRequestEntity($entity) |
75
|
|
|
->setResponseEntity(new DynamicResponse()) |
76
|
|
|
->build() |
77
|
|
|
; |
78
|
|
|
|
79
|
|
|
return $this->getHttpClient()->execute($request); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
* |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function removeProduct(ProductRemovedRequestEntity $entity) |
88
|
|
|
{ |
89
|
|
|
$this->configuration->assertLoaded(); |
90
|
|
|
|
91
|
|
|
$url = $this->getProductUrl($entity->getExternalId()); |
92
|
|
|
|
93
|
|
|
$request = RequestBuilder::delete($url) |
94
|
|
|
->contentTypeIsJson() |
95
|
|
|
->addRawHeader( |
96
|
|
|
$this->getToken(OauthTokenInterface::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
97
|
|
|
) |
98
|
|
|
->setRequestEntity($entity) |
99
|
|
|
->setResponseEntity(new DynamicResponse()) |
100
|
|
|
->build() |
101
|
|
|
; |
102
|
|
|
|
103
|
|
|
return $this->getHttpClient()->execute($request); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function exportProducts(ProductsIteratorInterface $productsIterator, $externalId) |
110
|
|
|
{ |
111
|
|
|
$this->configuration->assertLoaded(); |
112
|
|
|
$successCount = 0; |
113
|
|
|
|
114
|
|
|
foreach ($productsIterator as $productRequestEntity) { |
115
|
|
|
try { |
116
|
|
|
$productRequestEntity->setExternalId($externalId); |
117
|
|
|
$this->createProduct($productRequestEntity); |
118
|
|
|
$successCount++; |
119
|
|
|
} catch (\Exception $exception) { |
120
|
|
|
$this->configuration->getLogger() |
121
|
|
|
->critical( |
122
|
|
|
sprintf( |
123
|
|
|
'Product SKU=%s failed to export: %s', |
124
|
|
|
$productRequestEntity->getSku(), |
125
|
|
|
$exception->getMessage() |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $successCount; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $externalId |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function getProductUrl($externalId) |
139
|
|
|
{ |
140
|
|
|
return $this->getThirdPartyBaseUrl() . sprintf(static::SUB_URL_PRODUCT, $externalId); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|