Passed
Push — feature/eco-2295/eco-2344-crea... ( cf26a9...497ce3 )
by Aleksey
03:09
created

convertBasketItemTransferToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\CrefoPayApi\Business\Request\Converter;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CrefoPayApiAmountTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiAmountTransfer 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 Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayApiBasketItemTransfer 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...
13
use Generated\Shared\Transfer\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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...
14
use Generated\Shared\Transfer\CrefoPayApiReserveInformationRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ormationRequestTransfer 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...
15
use SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig;
16
17
class ReserveRequestConverter implements CrefoPayApiRequestConverterInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
21
     */
22
    protected $config;
23
24
    /**
25
     * @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config
26
     */
27
    public function __construct(CrefoPayApiConfig $config)
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
34
     *
35
     * @return array
36
     */
37
    public function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array
38
    {
39
        $reserveRequest = $requestTransfer->getReserveRequest();
40
        if ($reserveRequest === null) {
41
            return [];
42
        }
43
44
        return [
45
            $this->config->getApiFieldMerchantId() => $reserveRequest->getMerchantID(),
46
            $this->config->getApiFieldStoreId() => $reserveRequest->getStoreID(),
47
            $this->config->getApiFieldOrderId() => $reserveRequest->getOrderID(),
48
            $this->config->getApiFieldPaymentMethod() => $reserveRequest->getPaymentMethod(),
49
            $this->config->getApiFieldPaymentInstrumentId() => $reserveRequest->getPaymentInstrumentID(),
50
            $this->config->getApiFieldAdditionalInformation() => $this->getAdditionalInformationData($reserveRequest->getAdditionalInformation()),
51
            $this->config->getApiFieldAmount() => $this->getAmountData($reserveRequest->getAmount()),
52
            $this->config->getApiFieldBasketItems() => $this->getBasketItemsData($reserveRequest->getBasketItems()),
53
            $this->config->getApiFieldCvv() => $reserveRequest->getCvv(),
54
        ];
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\CrefoPayApiReserveInformationRequestTransfer|null $reserveInformationRequestTransfer
59
     *
60
     * @return array|null
61
     */
62
    protected function getAdditionalInformationData(?CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer): ?array
63
    {
64
        return $reserveInformationRequestTransfer ? $this->convertAdditionalInformationTransferToArray($reserveInformationRequestTransfer) : null;
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer
69
     *
70
     * @return array
71
     */
72
    protected function convertAdditionalInformationTransferToArray(CrefoPayApiReserveInformationRequestTransfer $reserveInformationRequestTransfer): array
73
    {
74
        return [
75
            $this->config->getApiObjectAdditionalInformationFieldSalutation() => $reserveInformationRequestTransfer->getSalutation(),
76
            $this->config->getApiObjectAdditionalInformationFieldDateOfBirth() => $reserveInformationRequestTransfer->getDateOfBirth(),
77
        ];
78
    }
79
80
    /**
81
     * @param \ArrayObject|\Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer[] $basketItems
82
     *
83
     * @return array|null
84
     */
85
    protected function getBasketItemsData(ArrayObject $basketItems): ?array
86
    {
87
        if ($basketItems->count() === 0) {
88
            return null;
89
        }
90
91
        return array_map(
92
            function (CrefoPayApiBasketItemTransfer $basketItem) {
93
                return $this->convertBasketItemTransferToArray($basketItem);
94
            },
95
            $basketItems->getArrayCopy()
96
        );
97
    }
98
99
    /**
100
     * @param \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer|null $basketItem
101
     *
102
     * @return array
103
     */
104
    protected function convertBasketItemTransferToArray(?CrefoPayApiBasketItemTransfer $basketItem): array
105
    {
106
        if ($basketItem === null) {
107
            return [];
108
        }
109
110
        return [
111
            $this->config->getApiObjectBasketItemFieldText() => $basketItem->getBasketItemText(),
112
            $this->config->getApiObjectBasketItemFieldId() => $basketItem->getBasketItemID(),
113
            $this->config->getApiObjectBasketItemFieldCount() => $basketItem->getBasketItemCount(),
114
            $this->config->getApiObjectBasketItemFieldAmount() => $this->getAmountData($basketItem->getBasketItemAmount()),
115
            $this->config->getApiObjectBasketItemFieldRiskClass() => $basketItem->getBasketItemRiskClass(),
116
            $this->config->getApiObjectBasketItemFieldType() => $basketItem->getBasketItemType(),
117
        ];
118
    }
119
120
    /**
121
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null $amountTransfer
122
     *
123
     * @return array|null
124
     */
125
    protected function getAmountData(?CrefoPayApiAmountTransfer $amountTransfer): ?array
126
    {
127
        return $amountTransfer ? $this->convertAmountTransferToArray($amountTransfer) : null;
128
    }
129
130
    /**
131
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer $amountTransfer
132
     *
133
     * @return array
134
     */
135
    protected function convertAmountTransferToArray(CrefoPayApiAmountTransfer $amountTransfer): array
136
    {
137
        return [
138
            $this->config->getApiObjectAmountFieldAmount() => $amountTransfer->getAmount(),
139
            $this->config->getApiObjectAmountFieldVatAmount() => $amountTransfer->getVatAmount(),
140
            $this->config->getApiObjectAmountFieldVatRate() => $amountTransfer->getVatRate(),
141
        ];
142
    }
143
}
144