NotificationRequestProcessor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 38
c 1
b 0
f 0
dl 0
loc 102
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A processNotification() 0 45 5
A getRequestPayload() 0 3 1
A unserializePayload() 0 3 1
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Notification
7
 * @package   Payever\Payments
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\Payments\Notification;
16
17
use Payever\ExternalIntegration\Core\Lock\LockInterface;
18
use Payever\ExternalIntegration\Payments\Http\RequestEntity\NotificationRequestEntity;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * @SuppressWarnings(PHPMD.MissingImport)
23
 */
24
class NotificationRequestProcessor
25
{
26
    const NOTIFICATION_LOCK_SECONDS = 30;
27
    const LOG_PREFIX = '[Notification]';
28
29
    /** @var NotificationHandlerInterface */
30
    protected $handler;
31
32
    /** @var LockInterface */
33
    protected $lock;
34
35
    /** @var LoggerInterface */
36
    protected $logger;
37
38
    /**
39
     * NotificationService constructor.
40
     *
41
     * @param NotificationHandlerInterface $handler
42
     * @param LockInterface $lock
43
     * @param LoggerInterface $logger
44
     */
45
    public function __construct(
46
        NotificationHandlerInterface $handler,
47
        LockInterface $lock,
48
        LoggerInterface $logger
49
    ) {
50
        $this->handler = $handler;
51
        $this->lock = $lock;
52
        $this->logger = $logger;
53
    }
54
55
    /**
56
     * @param string|null $payload
57
     *
58
     * @return NotificationResult
59
     *
60
     * @throws \RuntimeException when couldn't get payload
61
     * @throws \UnexpectedValueException when notification validation failed
62
     */
63
    public function processNotification($payload = null)
64
    {
65
        if (is_null($payload)) {
66
            $payload = $this->getRequestPayload();
67
        }
68
69
        if (empty($payload)) {
70
            throw new \RuntimeException('Got empty notification payload', 20);
71
        }
72
73
        $notificationResult = new NotificationResult();
74
        $notificationEntity = $this->unserializePayload($payload);
75
76
        if (!$notificationEntity->isValid()) {
77
            throw new \UnexpectedValueException('Notification entity is invalid', 21);
78
        }
79
80
        $paymentId = $notificationEntity->getPayment()->getId();
81
82
        $this->logger->debug(sprintf('%s Attempting to lock %s', static::LOG_PREFIX, $paymentId));
83
        $this->lock->acquireLock($paymentId, static::NOTIFICATION_LOCK_SECONDS);
84
        $this->logger->debug(sprintf('%s Locked  %s', static::LOG_PREFIX, $paymentId));
85
86
        try {
87
            $this->handler->handleNotification($notificationEntity, $notificationResult);
88
        } catch (\Exception $exception) {
89
            $this->logger->critical(
90
                sprintf('%s Exception while handling notification: %s', static::LOG_PREFIX, $exception->getMessage())
91
            );
92
            $notificationResult->addException($exception);
93
        }
94
95
        $this->lock->releaseLock($paymentId);
96
        $this->logger->debug(sprintf('%s Unlocked  %s', static::LOG_PREFIX, $paymentId));
97
98
        $this->logger->info(
99
            sprintf(
100
                '%s Processed notification for payment %s: %s',
101
                static::LOG_PREFIX,
102
                $paymentId,
103
                (string) $notificationResult
104
            )
105
        );
106
107
        return $notificationResult;
108
    }
109
110
    /**
111
     * @param string $payload
112
     *
113
     * @return NotificationRequestEntity
114
     */
115
    protected function unserializePayload($payload)
116
    {
117
        return new NotificationRequestEntity(json_decode($payload, true));
118
    }
119
120
    /**
121
     * @return false|string
122
     */
123
    protected function getRequestPayload()
124
    {
125
        return file_get_contents('php://input');
126
    }
127
}
128