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