Passed
Branch master (776013)
by payever
03:51
created

ActionRequestProcessor::processActionRequest()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 44
rs 9.488
c 0
b 0
f 0
cc 3
nc 8
nop 2
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 ActionRequestProcessor
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 \Payever\ExternalIntegration\ThirdParty\Enum\Action}
49
     * @param 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 processActionRequest($action, $payload = null)
54
    {
55
        $loggerPrefix = '[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(
73
                    '%s Action request payload: %s',
74
                    $loggerPrefix,
75
                    $actionPayload->getRawPayload()
76
                )
77
            );
78
79
            $handler->handle($actionPayload, $this->actionResult);
80
        } catch (\Exception $exception) {
81
            $this->logger->critical(
82
                sprintf(
83
                    '%s Processing action failed. EXCEPTION: %s: %s',
84
                    $loggerPrefix,
85
                    $exception->getCode(),
86
                    $exception->getMessage()
87
                ),
88
                $this->getFinishLogContext($action)
89
            );
90
91
            throw $exception;
92
        }
93
94
        $this->logger->info(
95
            sprintf('%s Processed action request', $loggerPrefix),
96
            $this->getFinishLogContext($action)
97
        );
98
    }
99
100
    /**
101
     * @param string $action
102
     *
103
     * @return array
104
     */
105
    protected function getFinishLogContext($action)
106
    {
107
        return array(
108
            'action' => $action,
109
            'result' => $this->actionResult->toString(),
110
            'errors' => $this->actionResult->getErrors(),
111
        );
112
    }
113
}
114