TransactionLogger::isLoggingEnabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction\Logger;
9
10
use Generated\Shared\Transfer\AmazonpayPaymentTransfer;
11
use Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer;
12
use Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpayApiLog;
13
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface;
14
15
class TransactionLogger implements TransactionLoggerInterface
16
{
17
    public const REPORT_LEVEL_ALL = 'ALL';
18
    public const REPORT_LEVEL_ERRORS_ONLY = 'ERRORS_ONLY';
19
    public const REPORT_LEVEL_DISABLED = 'DISABLED';
20
21
    /**
22
     * @var string
23
     */
24
    protected $reportLevel;
25
26
    /**
27
     * @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config
28
     */
29
    public function __construct(AmazonPayConfigInterface $config)
30
    {
31
        $this->reportLevel = $config->getErrorReportLevel();
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    protected function loggingEnabledMap()
38
    {
39
        return [
40
            static::REPORT_LEVEL_ALL => function () {
41
                return true;
42
            },
43
            static::REPORT_LEVEL_DISABLED => function () {
44
                return false;
45
            },
46
            static::REPORT_LEVEL_ERRORS_ONLY => function (AmazonpayResponseHeaderTransfer $headerTransfer) {
47
                return !$headerTransfer->getIsSuccess();
48
            },
49
        ];
50
    }
51
52
    /**
53
     * @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $headerTransfer
54
     *
55
     * @return bool
56
     */
57
    protected function isLoggingEnabled(AmazonpayResponseHeaderTransfer $headerTransfer)
58
    {
59
        return $this->loggingEnabledMap()[$this->reportLevel]($headerTransfer);
60
    }
61
62
    /**
63
     * @param \Generated\Shared\Transfer\AmazonpayPaymentTransfer $amazonpayPaymentTransfer
64
     *
65
     * @return void
66
     */
67
    public function log(AmazonpayPaymentTransfer $amazonpayPaymentTransfer)
68
    {
69
        if (!$this->isLoggingEnabled($amazonpayPaymentTransfer->getResponseHeader())) {
70
            return;
71
        }
72
73
        $this->storeLogEntry($amazonpayPaymentTransfer->getOrderReferenceId(), $amazonpayPaymentTransfer->getResponseHeader());
74
    }
75
76
    /**
77
     * @param string $orderReferenceId
78
     * @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $headerTransfer
79
     *
80
     * @return void
81
     */
82
    protected function storeLogEntry($orderReferenceId, AmazonpayResponseHeaderTransfer $headerTransfer)
83
    {
84
        $logEntity = new SpyPaymentAmazonpayApiLog();
85
        $logEntity->setOrderReferenceId($orderReferenceId);
86
        $logEntity->setStatusCode($headerTransfer->getStatusCode());
87
        $logEntity->setRequestId($headerTransfer->getRequestId());
88
        $logEntity->setErrorMessage($headerTransfer->getErrorMessage());
89
        $logEntity->setErrorCode($headerTransfer->getErrorCode());
90
        $logEntity->save();
91
    }
92
}
93