Passed
Pull Request — dev (#9)
by Andrey
07:03 queued 03:30
created

UpdateOrderRefundStatusTransaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 13.95 %

Importance

Changes 0
Metric Value
dl 12
loc 86
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 11 11 2
A __construct() 0 11 1
B updatePayment() 0 26 6
A isAllowed() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
9
10
use Generated\Shared\Transfer\AmazonpayCallTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayCallTransfer 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 SprykerEco\Shared\AmazonPay\AmazonPayConfig;
12
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface;
13
use SprykerEco\Zed\AmazonPay\Business\Api\Adapter\CallAdapterInterface;
14
use SprykerEco\Zed\AmazonPay\Business\Order\PaymentProcessorInterface;
15
use SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface;
16
use SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface;
17
18
class UpdateOrderRefundStatusTransaction extends AbstractAmazonpayTransaction
19
{
20
    /**
21
     * @var \SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface
22
     */
23
    protected $refundOrderModel;
24
25
    /**
26
     * @param \SprykerEco\Zed\AmazonPay\Business\Api\Adapter\CallAdapterInterface $executionAdapter
27
     * @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config
28
     * @param \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface $transactionLogger
29
     * @param \SprykerEco\Zed\AmazonPay\Business\Order\PaymentProcessorInterface $paymentProcessor
30
     * @param \SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface $refundOrderModel
31
     */
32
    public function __construct(
33
        CallAdapterInterface $executionAdapter,
34
        AmazonPayConfigInterface $config,
35
        TransactionLoggerInterface $transactionLogger,
36
        PaymentProcessorInterface $paymentProcessor,
37
        RefundOrderInterface $refundOrderModel
38
    ) {
39
40
        parent::__construct($executionAdapter, $config, $transactionLogger, $paymentProcessor);
41
42
        $this->refundOrderModel = $refundOrderModel;
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
47
     *
48
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
49
     */
50 View Code Duplication
    public function execute(AmazonpayCallTransfer $amazonPayCallTransfer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        if (!$this->isAllowed($amazonPayCallTransfer)) {
53
            return $amazonPayCallTransfer;
54
        }
55
56
        $amazonPayCallTransfer = parent::execute($amazonPayCallTransfer);
57
58
        $this->updatePayment($amazonPayCallTransfer);
59
60
        return $amazonPayCallTransfer;
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
65
     *
66
     * @return bool
67
     */
68
    protected function isAllowed(AmazonpayCallTransfer $amazonPayCallTransfer)
69
    {
70
        return !empty($amazonPayCallTransfer->getAmazonpayPayment()->getRefundDetails()->getAmazonRefundId());
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
75
     *
76
     * @return void
77
     */
78
    protected function updatePayment(AmazonpayCallTransfer $amazonPayCallTransfer)
79
    {
80
        if (!$this->apiResponse->getResponseHeader()->getIsSuccess()) {
81
            return;
82
        }
83
84
        $isPartialProcessing = $this->isPartialProcessing($this->paymentEntity, $amazonPayCallTransfer);
85
86
        if ($isPartialProcessing) {
87
            $this->paymentEntity = $this->paymentProcessor->duplicatePaymentEntity($this->paymentEntity);
88
        }
89
90
        $status = $this->apiResponse->getRefundDetails()->getRefundStatus()->getState();
91
92
        $refundIsRequired = ($status === AmazonPayConfig::STATUS_COMPLETED
93
            && $this->paymentEntity->getStatus() !== $status);
94
95
        $this->paymentEntity->setStatus($status);
96
        $this->paymentEntity->save();
97
98
        if ($isPartialProcessing) {
99
            $this->paymentProcessor->assignAmazonpayPaymentToItems($this->paymentEntity, $amazonPayCallTransfer);
100
        }
101
102
        if ($refundIsRequired) {
103
            $this->refundOrderModel->refundPayment($this->paymentEntity);
104
        }
105
    }
106
}
107