Passed
Branch feature/ECO-573-per-item-proce... (fe5bf4)
by Andrey
04:56
created

CaptureOrderTransaction::execute()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 11
nop 1
dl 0
loc 53
rs 7.1199
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
11
use Generated\Shared\Transfer\AmazonpayStatusTransfer;
12
use SprykerEco\Shared\Amazonpay\AmazonpayConstants;
13
14
class CaptureOrderTransaction extends AbstractAmazonpayTransaction
15
{
16
17
    /**
18
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
19
     *
20
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
21
     */
22
    public function execute(AmazonpayCallTransfer $amazonpayCallTransfer)
23
    {
24
        if (!in_array($amazonpayCallTransfer->getAmazonpayPayment()->getStatus(), [
25
            AmazonpayConstants::OMS_STATUS_CAPTURE_PENDING,
26
            AmazonpayConstants::OMS_STATUS_AUTH_OPEN,
27
            AmazonpayConstants::OMS_STATUS_PAYMENT_METHOD_CHANGED,
28
        ], true)) {
29
            return $amazonpayCallTransfer;
30
        }
31
32
        if ($amazonpayCallTransfer->getAmazonpayPayment()->getCaptureDetails()
33
            && $amazonpayCallTransfer->getAmazonpayPayment()->getCaptureDetails()->getAmazonCaptureId()) {
34
            return $amazonpayCallTransfer;
35
        }
36
37
        $amazonpayCallTransfer->getAmazonpayPayment()->getCaptureDetails()->setCaptureReferenceId(
38
            $this->generateOperationReferenceId($amazonpayCallTransfer)
39
        );
40
41
        $amazonpayCallTransfer = parent::execute($amazonpayCallTransfer);
42
43
        if (!$amazonpayCallTransfer->getAmazonpayPayment()->getResponseHeader()->getIsSuccess()) {
44
            return $amazonpayCallTransfer;
45
        }
46
47
        $isPartialProcessing = $this->isPartialProcessing($this->paymentEntity, $amazonpayCallTransfer);
48
49
        if ($isPartialProcessing) {
50
            $this->paymentEntity = $this->duplicatePaymentEntity($this->paymentEntity);
51
        }
52
53
        $amazonpayCallTransfer->getAmazonpayPayment()->setCaptureDetails(
54
            $this->apiResponse->getCaptureDetails()
55
        );
56
        $this->paymentEntity->setAmazonCaptureId(
57
            $this->apiResponse->getCaptureDetails()->getAmazonCaptureId()
58
        );
59
        $this->paymentEntity->setCaptureReferenceId(
60
            $this->apiResponse->getCaptureDetails()->getCaptureReferenceId()
61
        );
62
        $newStatus = $this->getPaymentStatus($amazonpayCallTransfer->getAmazonpayPayment()->getCaptureDetails()->getCaptureStatus());
63
64
        if ($newStatus !== '') {
65
            $this->paymentEntity->setStatus($newStatus);
66
        }
67
68
        $this->paymentEntity->save();
69
70
        if ($isPartialProcessing) {
71
            $this->assignAmazonpayPaymentToItemsIfNew($this->paymentEntity, $amazonpayCallTransfer);
72
        }
73
74
        return $amazonpayCallTransfer;
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\AmazonpayStatusTransfer $captureStatus
79
     *
80
     * @return string
81
     */
82 View Code Duplication
    protected function getPaymentStatus(AmazonpayStatusTransfer $captureStatus)
83
    {
84
        if ($captureStatus->getIsDeclined()) {
85
            return AmazonpayConstants::OMS_STATUS_CAPTURE_DECLINED;
86
        }
87
88
        if ($captureStatus->getIsPending()) {
89
            return AmazonpayConstants::OMS_STATUS_CAPTURE_PENDING;
90
        }
91
92
        if ($captureStatus->getIsCompleted()) {
93
            return AmazonpayConstants::OMS_STATUS_CAPTURE_COMPLETED;
94
        }
95
96
        return '';
97
    }
98
99
}
100