AbstractConverter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 58
c 1
b 1
f 0
dl 0
loc 154
rs 10
wmc 12

7 Methods

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