|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 5.4 and 7 |
|
4
|
|
|
* |
|
5
|
|
|
* @package Payever\ThirdParty |
|
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\ThirdParty\Action; |
|
12
|
|
|
|
|
13
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity; |
|
14
|
|
|
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity; |
|
15
|
|
|
use Payever\ExternalIntegration\Inventory\InventoryApiClient; |
|
16
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity; |
|
17
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity; |
|
18
|
|
|
use Payever\ExternalIntegration\Products\ProductsApiClient; |
|
19
|
|
|
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum; |
|
20
|
|
|
use Psr\Log\LoggerInterface; |
|
21
|
|
|
|
|
22
|
|
|
class OutwardActionProcessor |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ProductsApiClient */ |
|
25
|
|
|
private $productsApiClient; |
|
26
|
|
|
|
|
27
|
|
|
/** @var InventoryApiClient */ |
|
28
|
|
|
private $inventoryApiClient; |
|
29
|
|
|
|
|
30
|
|
|
/** @var LoggerInterface */ |
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
ProductsApiClient $productsApiClient, |
|
35
|
|
|
InventoryApiClient $inventoryApiClient, |
|
36
|
|
|
LoggerInterface $logger |
|
37
|
|
|
) { |
|
38
|
|
|
$this->productsApiClient = $productsApiClient; |
|
39
|
|
|
$this->inventoryApiClient = $inventoryApiClient; |
|
40
|
|
|
$this->logger = $logger; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $action - {@see ActionEnum} |
|
45
|
|
|
* @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \RuntimeException - when given action is unknown |
|
48
|
|
|
* @throws \Exception - bubbles up anything thrown by API |
|
49
|
|
|
*/ |
|
50
|
|
|
public function process($action, $payload) |
|
51
|
|
|
{ |
|
52
|
|
|
$loggerPrefix = '[OUTWARD_ACTION_REQUEST]'; |
|
53
|
|
|
|
|
54
|
|
|
$this->logger->info( |
|
55
|
|
|
sprintf('%s Processing action request', $loggerPrefix), |
|
56
|
|
|
compact('action') |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$this->logger->debug( |
|
60
|
|
|
sprintf('%s Action request payload', $loggerPrefix), |
|
61
|
|
|
compact($payload) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$this->executeActionRequest($action, $payload); |
|
66
|
|
|
} catch (\Exception $exception) { |
|
67
|
|
|
$this->logger->critical( |
|
68
|
|
|
sprintf( |
|
69
|
|
|
'%s Processing action failed. EXCEPTION: %s: %s', |
|
70
|
|
|
$loggerPrefix, |
|
71
|
|
|
$exception->getCode(), |
|
72
|
|
|
$exception->getMessage() |
|
73
|
|
|
), |
|
74
|
|
|
compact('action', 'payload') |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
throw $exception; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $action - {@see ActionEnum} |
|
83
|
|
|
* @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Exception |
|
86
|
|
|
*/ |
|
87
|
|
|
private function executeActionRequest($action, $payload) |
|
88
|
|
|
{ |
|
89
|
|
|
switch ($action) { |
|
90
|
|
|
case ActionEnum::ACTION_SET_INVENTORY: |
|
91
|
|
|
$this->inventoryApiClient->createInventory( |
|
92
|
|
|
$payload instanceof InventoryCreateRequestEntity ? $payload : new InventoryCreateRequestEntity($payload) |
|
|
|
|
|
|
93
|
|
|
); |
|
94
|
|
|
break; |
|
95
|
|
|
case ActionEnum::ACTION_ADD_INVENTORY: |
|
96
|
|
|
$this->inventoryApiClient->addInventory( |
|
97
|
|
|
$payload instanceof InventoryChangedRequestEntity ? $payload : new InventoryChangedRequestEntity($payload) |
|
98
|
|
|
); |
|
99
|
|
|
break; |
|
100
|
|
|
case ActionEnum::ACTION_SUBTRACT_INVENTORY: |
|
101
|
|
|
$this->inventoryApiClient->subtractInventory( |
|
102
|
|
|
$payload instanceof InventoryChangedRequestEntity ? $payload : new InventoryChangedRequestEntity($payload) |
|
103
|
|
|
); |
|
104
|
|
|
break; |
|
105
|
|
|
case ActionEnum::ACTION_CREATE_PRODUCT: |
|
106
|
|
|
$this->productsApiClient->createProduct( |
|
107
|
|
|
$payload instanceof ProductRequestEntity ? $payload : new ProductRequestEntity($payload) |
|
108
|
|
|
); |
|
109
|
|
|
break; |
|
110
|
|
|
case ActionEnum::ACTION_UPDATE_PRODUCT: |
|
111
|
|
|
$this->productsApiClient->updateProduct( |
|
112
|
|
|
$payload instanceof ProductRequestEntity ? $payload : new ProductRequestEntity($payload) |
|
113
|
|
|
); |
|
114
|
|
|
break; |
|
115
|
|
|
case ActionEnum::ACTION_REMOVE_PRODUCT: |
|
116
|
|
|
$this->productsApiClient->removeProduct( |
|
117
|
|
|
$payload instanceof ProductRemovedRequestEntity ? $payload : new ProductRemovedRequestEntity($payload) |
|
118
|
|
|
); |
|
119
|
|
|
break; |
|
120
|
|
|
default: |
|
121
|
|
|
throw new \RuntimeException(sprintf('Unknown action %s', $action)); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|