1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Action |
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\Action; |
16
|
|
|
|
17
|
|
|
use Payever\ExternalIntegration\Core\Base\MessageEntity; |
18
|
|
|
use Payever\ExternalIntegration\Inventory\Http\MessageEntity\InventoryChangedEntity; |
19
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity; |
20
|
|
|
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity; |
21
|
|
|
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @SuppressWarnings(PHPMD.MissingImport) |
25
|
|
|
*/ |
26
|
|
|
class ActionPayload |
27
|
|
|
{ |
28
|
|
|
/** @var string */ |
29
|
|
|
protected $action; |
30
|
|
|
|
31
|
|
|
/** @var bool|string|array */ |
32
|
|
|
protected $rawPayload; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $action @see ActionEnum |
36
|
|
|
* @param string|array|null $rawPayload |
37
|
|
|
*/ |
38
|
|
|
public function __construct($action, $rawPayload = null) |
39
|
|
|
{ |
40
|
|
|
$this->action = $action; |
41
|
|
|
$this->rawPayload = $rawPayload; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return MessageEntity |
46
|
|
|
* |
47
|
|
|
* @throws \UnexpectedValueException when can't fetch request payload |
48
|
|
|
* @throws \RuntimeException when can't map action to payload entity |
49
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
50
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
51
|
|
|
*/ |
52
|
|
|
public function getPayloadEntity() |
53
|
|
|
{ |
54
|
|
|
if (!$this->rawPayload) { |
55
|
|
|
$this->rawPayload = $this->getRequestPayload(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!$this->rawPayload) { |
59
|
|
|
throw new \UnexpectedValueException('Got empty action request payload.', 40); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_string($this->rawPayload)) { |
63
|
|
|
$payload = $this->unserializePayload($this->rawPayload); |
64
|
|
|
} else { |
65
|
|
|
$payload = $this->rawPayload; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset($payload['payload'])) { |
69
|
|
|
$payload = $payload['payload']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
switch ($this->action) { |
73
|
|
|
case ActionEnum::ACTION_CREATE_PRODUCT: |
74
|
|
|
case ActionEnum::ACTION_UPDATE_PRODUCT: |
75
|
|
|
return new ProductRequestEntity($payload); |
76
|
|
|
case ActionEnum::ACTION_REMOVE_PRODUCT: |
77
|
|
|
return new ProductRemovedRequestEntity($payload); |
78
|
|
|
case ActionEnum::ACTION_ADD_INVENTORY: |
79
|
|
|
case ActionEnum::ACTION_SET_INVENTORY: |
80
|
|
|
case ActionEnum::ACTION_SUBTRACT_INVENTORY: |
81
|
|
|
return new InventoryChangedEntity($payload); |
82
|
|
|
default: |
83
|
|
|
throw new \RuntimeException( |
84
|
|
|
sprintf('Payload entity not found for action %s', $this->action), |
85
|
|
|
41 |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return false|array|string |
92
|
|
|
*/ |
93
|
|
|
public function getRawPayload() |
94
|
|
|
{ |
95
|
|
|
return $this->rawPayload ?: $this->getRequestPayload(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $payload |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function unserializePayload($payload) |
103
|
|
|
{ |
104
|
|
|
return json_decode($payload, true); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return false|string |
109
|
|
|
*/ |
110
|
|
|
protected function getRequestPayload() |
111
|
|
|
{ |
112
|
|
|
return file_get_contents('php://input'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|