Completed
Pull Request — master (#4)
by Andrey
10:46 queued 03:14
created

CaptureOrderTransaction   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 18.07 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 53 8
A getPaymentStatus() 15 15 4

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;
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