Completed
Pull Request — master (#4)
by Andrey
11:13 queued 07:59
created

AbstractConverter::convertStatusToTransfer()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 61
Code Lines 32

Duplication

Lines 8
Ratio 13.11 %

Importance

Changes 0
Metric Value
cc 6
eloc 32
nc 16
nop 1
dl 8
loc 61
rs 8.6806
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\Api\Converter;
9
10
use Generated\Shared\Transfer\AmazonpayPriceTransfer;
11
use Generated\Shared\Transfer\AmazonpayStatusTransfer;
12
use Spryker\Shared\Kernel\Transfer\TransferInterface;
13
use SprykerEco\Shared\Amazonpay\AmazonpayConstants;
14
15
abstract class AbstractConverter
16
{
17
18
    const STATUS_DECLINED = 'Declined';
19
    const STATUS_PENDING = 'Pending';
20
    const STATUS_OPEN = 'Open';
21
    const STATUS_CLOSED = 'Closed';
22
    const STATUS_COMPLETED = 'Completed';
23
    const STATUS_SUSPENDED = 'Suspended';
24
    const STATUS_CANCELLED = 'Canceled';
25
    const LAST_UPDATE_TIMESTAMP = 'LastUpdateTimestamp';
26
    const REASON_CODE = 'ReasonCode';
27
    const STATE = 'State';
28
29
    /**
30
     * @param array $priceData
31
     *
32
     * @return \Generated\Shared\Transfer\AmazonpayPriceTransfer
33
     */
34
    protected function convertPriceToTransfer(array $priceData)
35
    {
36
        $priceTransfer = new AmazonpayPriceTransfer();
37
38
        $priceTransfer->setAmount($priceData['Amount']);
39
        $priceTransfer->setCurrencyCode($priceData['CurrencyCode']);
40
41
        return $priceTransfer;
42
    }
43
44
    /**
45
     * @param array $statusData
46
     *
47
     * @return \Generated\Shared\Transfer\AmazonpayStatusTransfer
48
     */
49
    protected function convertStatusToTransfer(array $statusData)
50
    {
51
        $status = new AmazonpayStatusTransfer();
52
53
        if (!empty($statusData[self::LAST_UPDATE_TIMESTAMP])) {
54
            $status->setLastUpdateTimestamp($statusData[self::LAST_UPDATE_TIMESTAMP]);
55
        }
56
57
        $status->setState($statusData[self::STATE]);
58
59
        if (!empty($statusData[self::REASON_CODE])) {
60
            $status->setReasonCode($statusData[self::REASON_CODE]);
61
            $status->setIsReauthorizable(
62
                $statusData[self::REASON_CODE] === AmazonpayConstants::REASON_CODE_SELLER_CLOSED
63
                || $statusData[self::REASON_CODE] === AmazonpayConstants::REASON_CODE_EXPIRED_UNUSED
64
            );
65
66
            $status->setIsPaymentMethodInvalid(
67
                $statusData[self::REASON_CODE] === AmazonpayConstants::REASON_CODE_PAYMENT_METHOD_INVALID
68
            );
69
70
            $status->setIsClosedByAmazon(
71
                $statusData[self::REASON_CODE] === AmazonpayConstants::REASON_CODE_AMAZON_CLOSED
72
            );
73
74
            $status->setIsTransactionTimedOut(
75
                $statusData[self::REASON_CODE] === AmazonpayConstants::REASON_CODE_TRANSACTION_TIMED_OUT
76
            );
77
        }
78
79 View Code Duplication
        if ($statusData[self::STATE] === static::STATUS_DECLINED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
            $status->setIsSuspended($status->getIsPaymentMethodInvalid());
81
            $status->setIsDeclined(true);
82
        }
83
84 View Code Duplication
        if ($statusData[self::STATE] === static::STATUS_SUSPENDED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
            $status->setIsSuspended(true);
86
            $status->setIsDeclined(true);
87
        }
88
89
        $status->setIsPending(
90
            $statusData[self::STATE] === static::STATUS_PENDING
91
        );
92
93
        $status->setIsOpen(
94
            $statusData[self::STATE] === static::STATUS_OPEN
95
        );
96
97
        $status->setIsClosed(
98
            $statusData[self::STATE] === static::STATUS_CLOSED
99
        );
100
101
        $status->setIsCompleted(
102
            $statusData[self::STATE] === static::STATUS_COMPLETED
103
        );
104
105
        $status->setIsCancelled(
106
            $statusData[self::STATE] === static::STATUS_CANCELLED
107
        );
108
109
        return $status;
110
    }
111
112
    /**
113
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface|\Generated\Shared\Transfer\CustomerTransfer|\Generated\Shared\Transfer\AddressTransfer $transfer
114
     * @param string $name
115
     *
116
     * @return \Generated\Shared\Transfer\CustomerTransfer|\Generated\Shared\Transfer\AddressTransfer
117
     */
118
    protected function updateNameData(TransferInterface $transfer, $name)
119
    {
120
        $names = explode(' ', $name, 2);
121
122
        if (count($names) === 2) {
123
            $transfer->setFirstName($names[0]);
0 ignored issues
show
Bug introduced by
The method setFirstName() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\TransferInterface such as Generated\Shared\Transfer\AddressTransfer or Generated\Shared\Transfer\CustomerTransfer or Generated\Shared\Transfer\UserTransfer or Generated\Shared\Transfer\OrderTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
            $transfer->/** @scrutinizer ignore-call */ setFirstName($names[0]);
Loading history...
124
            $transfer->setLastName($names[1]);
0 ignored issues
show
Bug introduced by
The method setLastName() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. It seems like you code against a sub-type of Spryker\Shared\Kernel\Transfer\TransferInterface such as Generated\Shared\Transfer\AddressTransfer or Generated\Shared\Transfer\CustomerTransfer or Generated\Shared\Transfer\UserTransfer or Generated\Shared\Transfer\OrderTransfer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
            $transfer->/** @scrutinizer ignore-call */ setLastName($names[1]);
Loading history...
125
        } else {
126
            $transfer->setFirstName($name);
127
            $transfer->setLastName($name);
128
        }
129
130
        return $transfer;
131
    }
132
133
}
134