Passed
Branch feature/ECO-808-scrutinizer (bde35b)
by Andrey
07:45
created

TransactionLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 13 2
A isLoggingEnabled() 0 15 4
A __construct() 0 3 1
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\AmazonpayResponseHeaderTransfer;
11
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...
12
13
class TransactionLogger implements TransactionLoggerInterface
14
{
15
16
    const REPORT_LEVEL_ALL = 'ALL';
17
    const REPORT_LEVEL_ERRORS_ONLY = 'ERRORS_ONLY';
18
    const REPORT_LEVEL_DISABLED = 'DISABLED';
19
20
    /**
21
     * @var int
22
     */
23
    protected $reportLevel;
24
25
    /**
26
     * @param string $reportLevel
27
     */
28
    public function __construct($reportLevel)
29
    {
30
        $this->reportLevel = $reportLevel;
0 ignored issues
show
Documentation Bug introduced by
The property $reportLevel was declared of type integer, but $reportLevel is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
31
    }
32
33
    /**
34
     * @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $headerTransfer
35
     *
36
     * @return bool
37
     */
38
    protected function isLoggingEnabled(AmazonpayResponseHeaderTransfer $headerTransfer)
39
    {
40
        if ($this->reportLevel === self::REPORT_LEVEL_ALL) {
41
            return true;
42
        };
43
44
        if ($this->reportLevel === self::REPORT_LEVEL_DISABLED) {
45
            return false;
46
        };
47
48
        if ($this->reportLevel === self::REPORT_LEVEL_ERRORS_ONLY) {
49
            return !$headerTransfer->getIsSuccess();
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * @param string $orderReferenceId
57
     * @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $headerTransfer
58
     *
59
     * @return void
60
     */
61
    public function log($orderReferenceId, AmazonpayResponseHeaderTransfer $headerTransfer)
62
    {
63
        if (!$this->isLoggingEnabled($headerTransfer)) {
64
            return;
65
        }
66
67
        $logEntity = new SpyPaymentAmazonpayApiLog();
68
        $logEntity->setOrderReferenceId($orderReferenceId);
69
        $logEntity->setStatusCode($headerTransfer->getStatusCode());
70
        $logEntity->setRequestId($headerTransfer->getRequestId());
71
        $logEntity->setErrorMessage($headerTransfer->getErrorMessage());
72
        $logEntity->setErrorCode($headerTransfer->getErrorCode());
73
        $logEntity->save();
74
    }
75
76
}
77