Completed
Pull Request — dev (#9)
by Andrey
09:24 queued 05:50
created

AbstractConverter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A convertStatusToTransfer() 0 18 3
A mapStatusToTransferProperties() 0 8 2
A convertPriceToTransfer() 0 8 1
A getStatusName() 0 3 1
A updateNameData() 0 13 2
A getStatusNameByReasonCode() 0 3 1
A getValueByKeyFromMap() 0 7 2
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;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayPriceTransfer 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 Generated\Shared\Transfer\AmazonpayStatusTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayStatusTransfer 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
use Spryker\Shared\Kernel\Transfer\TransferInterface;
13
use SprykerEco\Shared\AmazonPay\AmazonPayConfig;
14
15
abstract class AbstractConverter
16
{
17
    const STATUS_DECLINED = 'Declined';
18
    const STATUS_PENDING = 'Pending';
19
    const STATUS_OPEN = 'Open';
20
    const STATUS_CLOSED = 'Closed';
21
    const STATUS_COMPLETED = 'Completed';
22
    const STATUS_SUSPENDED = 'Suspended';
23
    const STATUS_CANCELLED = 'Canceled';
24
25
    const FIELD_LAST_UPDATE_TIMESTAMP = 'LastUpdateTimestamp';
26
    const FIELD_REASON_CODE = 'ReasonCode';
27
    const FIELD_STATE = 'State';
28
    const FIELD_AMOUNT = 'Amount';
29
    const FIELD_CURRENCY_CODE = 'CurrencyCode';
30
31
    const REASON_CODE_EXPIRED_UNUSED = 'ExpiredUnused';
32
    const REASON_CODE_SELLER_CLOSED = 'SellerClosed';
33
    const REASON_CODE_PAYMENT_METHOD_INVALID = 'InvalidPaymentMethod';
34
    const REASON_CODE_AMAZON_CLOSED = 'AmazonClosed';
35
    const REASON_CODE_TRANSACTION_TIMED_OUT = 'TransactionTimedOut';
36
37
    /**
38
     * @var array
39
     */
40
    protected $fieldToTransferMap = [
41
        self::FIELD_STATE => AmazonpayStatusTransfer::STATE,
42
        self::FIELD_REASON_CODE => AmazonpayStatusTransfer::REASON_CODE,
43
        self::FIELD_LAST_UPDATE_TIMESTAMP => AmazonpayStatusTransfer::LAST_UPDATE_TIMESTAMP,
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    protected $statusMap = [
50
        self::STATUS_DECLINED => AmazonPayConfig::STATUS_DECLINED,
51
        self::STATUS_SUSPENDED => AmazonPayConfig::STATUS_SUSPENDED,
52
        self::STATUS_PENDING => AmazonPayConfig::STATUS_PENDING,
53
        self::STATUS_OPEN => AmazonPayConfig::STATUS_OPEN,
54
        self::STATUS_CLOSED => AmazonPayConfig::STATUS_CLOSED,
55
        self::STATUS_COMPLETED => AmazonPayConfig::STATUS_COMPLETED,
56
        self::STATUS_CANCELLED => AmazonPayConfig::STATUS_CANCELLED,
57
    ];
58
59
    /**
60
     * @var array
61
     */
62
    protected $reasonToStatusMap = [
63
        self::REASON_CODE_AMAZON_CLOSED => AmazonPayConfig::STATUS_AMAZON_CLOSED,
64
        self::REASON_CODE_PAYMENT_METHOD_INVALID => AmazonPayConfig::STATUS_PAYMENT_METHOD_INVALID,
65
        self::REASON_CODE_TRANSACTION_TIMED_OUT => AmazonPayConfig::STATUS_TRANSACTION_TIMED_OUT,
66
        self::REASON_CODE_SELLER_CLOSED => AmazonPayConfig::STATUS_EXPIRED,
67
        self::REASON_CODE_EXPIRED_UNUSED => AmazonPayConfig::STATUS_EXPIRED,
68
    ];
69
70
    /**
71
     * @param array $priceData
72
     *
73
     * @return \Generated\Shared\Transfer\AmazonpayPriceTransfer
74
     */
75
    protected function convertPriceToTransfer(array $priceData)
76
    {
77
        $priceTransfer = new AmazonpayPriceTransfer();
78
79
        $priceTransfer->setAmount($priceData[static::FIELD_AMOUNT]);
80
        $priceTransfer->setCurrencyCode($priceData[static::FIELD_CURRENCY_CODE]);
81
82
        return $priceTransfer;
83
    }
84
85
    /**
86
     * @param array $statusData
87
     *
88
     * @return \Generated\Shared\Transfer\AmazonpayStatusTransfer
89
     */
90
    protected function convertStatusToTransfer(array $statusData)
91
    {
92
        $statusTransfer = new AmazonpayStatusTransfer();
93
94
        $mappedStatusData = $this->mapStatusToTransferProperties($statusData);
95
        $statusTransfer->fromArray($mappedStatusData, true);
96
97
        $statusName = $this->getStatusName($statusData);
98
        if ($statusName !== null) {
99
            $statusTransfer->setState($statusName);
100
        }
101
102
        $statusNameByReasonCode = $this->getStatusNameByReasonCode($statusData);
103
        if ($statusNameByReasonCode !== null) {
104
            $statusTransfer->setState($statusNameByReasonCode);
105
        }
106
107
        return $statusTransfer;
108
    }
109
110
    /**
111
     * @param array $statusData
112
     *
113
     * @return array
114
     */
115
    protected function mapStatusToTransferProperties(array $statusData)
116
    {
117
        $result = [];
118
        foreach ($this->fieldToTransferMap as $statusField => $propertyName) {
119
            $result[$propertyName] = $statusData[$statusField] ?? null;
120
        }
121
122
        return $result;
123
    }
124
125
    /**
126
     * @param array $statusData
127
     *
128
     * @return string|null
129
     */
130
    protected function getStatusName(array $statusData)
131
    {
132
        return $this->getValueByKeyFromMap($this->statusMap, $statusData, static::FIELD_STATE);
133
    }
134
135
    /**
136
     * @param array $statusData
137
     *
138
     * @return string|null
139
     */
140
    protected function getStatusNameByReasonCode(array $statusData)
141
    {
142
        return $this->getValueByKeyFromMap($this->reasonToStatusMap, $statusData, static::FIELD_REASON_CODE);
143
    }
144
145
    /**
146
     * @param array $map
147
     * @param array $statusData
148
     * @param string $key
149
     *
150
     * @return string|null
151
     */
152
    protected function getValueByKeyFromMap(array $map, array $statusData, $key)
153
    {
154
        if (!empty($statusData[$key])) {
155
            return $map[$statusData[$key]] ?? null;
156
        }
157
158
        return null;
159
    }
160
161
    /**
162
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface|\Generated\Shared\Transfer\CustomerTransfer|\Generated\Shared\Transfer\AddressTransfer $transfer
2 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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...
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer 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...
163
     * @param string $name
164
     *
165
     * @return \Generated\Shared\Transfer\CustomerTransfer|\Generated\Shared\Transfer\AddressTransfer
166
     */
167
    protected function updateNameData(TransferInterface $transfer, $name)
168
    {
169
        $names = explode(' ', $name, 2);
170
171
        if (count($names) === 2) {
172
            $transfer->setFirstName($names[0]);
0 ignored issues
show
Bug introduced by
The method setFirstName() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

172
            $transfer->/** @scrutinizer ignore-call */ 
173
                       setFirstName($names[0]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
            $transfer->setLastName($names[1]);
0 ignored issues
show
Bug introduced by
The method setLastName() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

173
            $transfer->/** @scrutinizer ignore-call */ 
174
                       setLastName($names[1]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
        } else {
175
            $transfer->setFirstName($name);
176
            $transfer->setLastName($name);
177
        }
178
179
        return $transfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transfer returns the type Spryker\Shared\Kernel\Transfer\TransferInterface which is incompatible with the documented return type Generated\Shared\Transfe...ansfer\CustomerTransfer.
Loading history...
180
    }
181
}
182