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