Completed
Pull Request — dev (#22)
by Andrey
08:00 queued 04:01
created

TransactionLogger::logMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayPaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...yResponseHeaderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpayApiLog;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\AmazonPay\Persis...yPaymentAmazonpayApiLog was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface;
14
15
class TransactionLogger implements TransactionLoggerInterface
16
{
17
    const REPORT_LEVEL_ALL = 'ALL';
18
    const REPORT_LEVEL_ERRORS_ONLY = 'ERRORS_ONLY';
19
    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 \Generated\Shared\Transfer\AmazonpayPaymentTransfer $amazonpayPaymentTransfer
78
     * @param string $message
79
     *
80
     * @return void
81
     */
82
    public function logMessage(AmazonpayPaymentTransfer $amazonpayPaymentTransfer, string $message)
83
    {
84
        $this->storeRawLogEntry($amazonpayPaymentTransfer->getOrderReferenceId(), $message);
85
    }
86
87
    /**
88
     * @param string $orderReferenceId
89
     * @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $headerTransfer
90
     *
91
     * @return void
92
     */
93
    protected function storeLogEntry($orderReferenceId, AmazonpayResponseHeaderTransfer $headerTransfer)
94
    {
95
        $logEntity = new SpyPaymentAmazonpayApiLog();
96
        $logEntity->setOrderReferenceId($orderReferenceId);
97
        $logEntity->setStatusCode($headerTransfer->getStatusCode());
98
        $logEntity->setRequestId($headerTransfer->getRequestId());
99
        $logEntity->setErrorMessage($headerTransfer->getErrorMessage());
100
        $logEntity->setErrorCode($headerTransfer->getErrorCode());
101
        $logEntity->save();
102
    }
103
104
    /**
105
     * @param string $orderReferenceId
106
     * @param string $message
107
     *
108
     * @return void
109
     */
110
    protected function storeRawLogEntry($orderReferenceId, string $message)
111
    {
112
        $logEntity = new SpyPaymentAmazonpayApiLog();
113
        $logEntity->setOrderReferenceId($orderReferenceId);
114
        $logEntity->setErrorMessage($message);
115
        $logEntity->save();
116
    }
117
}
118