Passed
Push — bugfix/ECO-1031-recent-review ( ec5ba1...60e7e8 )
by Andrey
03:23
created

AbstractConverter::getNameData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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 string $name
163
     *
164
     * @return array
165
     */
166
    protected function getNameData($name)
167
    {
168
        $names = explode(' ', $name, 2);
169
170
        if (count($names) === 2) {
171
            return $names;
172
        }
173
174
        return [$name, $name];
175
    }
176
}
177