|
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 Psr\Log\LoggerAwareInterface; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* PHP version 5.4 and 7 |
|
18
|
|
|
* |
|
19
|
|
|
* @package Payever\ThirdParty |
|
20
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
|
21
|
|
|
* @copyright 2017-2019 payever GmbH |
|
22
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
|
23
|
|
|
*/ |
|
24
|
|
|
class InwardActionProcessor |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ActionHandlerPool */ |
|
27
|
|
|
protected $actionHandlerPool; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ActionResult */ |
|
30
|
|
|
protected $actionResult; |
|
31
|
|
|
|
|
32
|
|
|
/** @var LoggerInterface */ |
|
33
|
|
|
protected $logger; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
ActionHandlerPool $actionHandlerPool, |
|
37
|
|
|
ActionResult $actionResult, |
|
38
|
|
|
LoggerInterface $logger |
|
39
|
|
|
) { |
|
40
|
|
|
$this->actionHandlerPool = $actionHandlerPool; |
|
41
|
|
|
$this->actionResult = $actionResult; |
|
42
|
|
|
$this->logger = $logger; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Do the job of processing payever third-party action request |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $action - action name {@see ActionEnum} |
|
49
|
|
|
* @param array|string|null $payload - user can pass payload directly if it's coming from custom source |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Exception - bubbles up anything thrown inside |
|
52
|
|
|
*/ |
|
53
|
|
|
public function process($action, $payload = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$loggerPrefix = '[INWARD_ACTION_REQUEST]'; |
|
56
|
|
|
|
|
57
|
|
|
$this->logger->info( |
|
58
|
|
|
sprintf('%s Processing action request', $loggerPrefix), |
|
59
|
|
|
compact('action') |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$actionPayload = new ActionPayload($action, $payload); |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$handler = $this->actionHandlerPool->getHandlerForAction($action); |
|
66
|
|
|
|
|
67
|
|
|
if ($handler instanceof LoggerAwareInterface) { |
|
68
|
|
|
$handler->setLogger($this->logger); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->logger->debug( |
|
72
|
|
|
sprintf('%s Action request payload', $loggerPrefix), |
|
73
|
|
|
array($actionPayload->getRawPayload()) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$handler->handle($actionPayload, $this->actionResult); |
|
77
|
|
|
} catch (\Exception $exception) { |
|
78
|
|
|
$this->logger->critical( |
|
79
|
|
|
sprintf( |
|
80
|
|
|
'%s Processing action failed. EXCEPTION: %s: %s', |
|
81
|
|
|
$loggerPrefix, |
|
82
|
|
|
$exception->getCode(), |
|
83
|
|
|
$exception->getMessage() |
|
84
|
|
|
), |
|
85
|
|
|
$this->getFinishLogContext($action) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
throw $exception; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->logger->info( |
|
92
|
|
|
sprintf('%s Processed action request', $loggerPrefix), |
|
93
|
|
|
$this->getFinishLogContext($action) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $action |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getFinishLogContext($action) |
|
103
|
|
|
{ |
|
104
|
|
|
return array( |
|
105
|
|
|
'action' => $action, |
|
106
|
|
|
'result' => $this->actionResult->toString(), |
|
107
|
|
|
'errors' => $this->actionResult->getErrors(), |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|