Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
07:54 queued 03:07
created

convertRequestTransferToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 25
nc 2
nop 1
dl 0
loc 29
rs 9.52
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\CrefoPayApiAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiAddressTransfer 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\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...
13
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...
14
use Generated\Shared\Transfer\CrefoPayApiCompanyTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiCompanyTransfer 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 Generated\Shared\Transfer\CrefoPayApiPersonTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiPersonTransfer 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...
16
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...
17
use SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig;
18
19
class CreateTransactionRequestConverter implements CrefoPayApiRequestConverterInterface
20
{
21
    /**
22
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
23
     */
24
    protected $config;
25
26
    /**
27
     * @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config
28
     */
29
    public function __construct(CrefoPayApiConfig $config)
30
    {
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
36
     *
37
     * @return array
38
     */
39
    public function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array
40
    {
41
        $createTransactionRequest = $requestTransfer->getCreateTransactionRequest();
42
        if ($createTransactionRequest === null) {
43
            return [];
44
        }
45
46
        return [
47
            $this->config->getApiFieldMerchantId() => $createTransactionRequest->getMerchantID(),
48
            $this->config->getApiFieldStoreId() => $createTransactionRequest->getStoreID(),
49
            $this->config->getApiFieldOrderId() => $createTransactionRequest->getOrderID(),
50
            $this->config->getApiFieldUserId() => $createTransactionRequest->getUserID(),
51
            $this->config->getApiFieldIntegrationType() => $createTransactionRequest->getIntegrationType(),
52
            $this->config->getApiFieldAutoCapture() => $createTransactionRequest->getAutoCapture(),
53
            $this->config->getApiFieldMerchantReference() => $createTransactionRequest->getMerchantReference(),
54
            $this->config->getApiFieldContext() => $createTransactionRequest->getContext(),
55
            $this->config->getApiFieldUserType() => $createTransactionRequest->getUserType(),
56
            $this->config->getApiFieldUserRiskClass() => $createTransactionRequest->getUserRiskClass(),
57
            $this->config->getApiFieldUserIpAddress() => $createTransactionRequest->getUserIpAddress(),
58
            $this->config->getApiFieldCompanyData() => $this->getCompanyData($createTransactionRequest->getCompanyData()),
59
            $this->config->getApiFieldUserData() => $this->getUserData($createTransactionRequest->getUserData()),
60
            $this->config->getApiFieldBillingRecipient() => $createTransactionRequest->getBillingRecipient(),
61
            $this->config->getApiFieldBillingAddress() => $this->getAddressData($createTransactionRequest->getBillingAddress()),
62
            $this->config->getApiFieldShippingRecipient() => $createTransactionRequest->getShippingRecipient(),
63
            $this->config->getApiFieldShippingAddress() => $this->getAddressData($createTransactionRequest->getShippingAddress()),
64
            $this->config->getApiFieldAmount() => $this->getAmountData($createTransactionRequest->getAmount()),
65
            $this->config->getApiFieldBasketItems() => $this->getBasketItemsData($createTransactionRequest->getBasketItems()),
66
            $this->config->getApiFieldBasketValidity() => $createTransactionRequest->getBasketValidity(),
67
            $this->config->getApiFieldLocale() => $createTransactionRequest->getLocale(),
68
        ];
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\CrefoPayApiCompanyTransfer|null $companyTransfer
73
     *
74
     * @return array|null
75
     */
76
    protected function getCompanyData(?CrefoPayApiCompanyTransfer $companyTransfer): ?array
77
    {
78
        return $companyTransfer ? $this->convertCompanyTransferToArray($companyTransfer) : null;
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\CrefoPayApiPersonTransfer|null $personTransfer
83
     *
84
     * @return array|null
85
     */
86
    protected function getUserData(?CrefoPayApiPersonTransfer $personTransfer): ?array
87
    {
88
        return $personTransfer ? $this->convertPersonTransferToArray($personTransfer) : null;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\CrefoPayApiAddressTransfer|null $addressTransfer
93
     *
94
     * @return array|null
95
     */
96
    protected function getAddressData(?CrefoPayApiAddressTransfer $addressTransfer): ?array
97
    {
98
        return $addressTransfer ? $this->convertAddressTransferToArray($addressTransfer) : null;
99
    }
100
101
    /**
102
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null $amountTransfer
103
     *
104
     * @return array|null
105
     */
106
    protected function getAmountData(?CrefoPayApiAmountTransfer $amountTransfer): ?array
107
    {
108
        return $amountTransfer ? $this->convertAmountTransferToArray($amountTransfer) : null;
109
    }
110
111
    /**
112
     * @param \ArrayObject|\Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer[] $basketItems
113
     *
114
     * @return array|null
115
     */
116
    protected function getBasketItemsData(ArrayObject $basketItems): ?array
117
    {
118
        if ($basketItems->count() === 0) {
119
            return null;
120
        }
121
122
        return array_map(
123
            function (CrefoPayApiBasketItemTransfer $basketItem) {
124
                return $this->convertBasketItemTransferToArray($basketItem);
125
            },
126
            $basketItems->getArrayCopy()
127
        );
128
    }
129
130
    /**
131
     * @param \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer $basketItem
132
     *
133
     * @return array
134
     */
135
    protected function convertBasketItemTransferToArray(CrefoPayApiBasketItemTransfer $basketItem): array
136
    {
137
        return [
138
            $this->config->getApiObjectBasketItemFieldText() => $basketItem->getBasketItemText(),
139
            $this->config->getApiObjectBasketItemFieldId() => $basketItem->getBasketItemID(),
140
            $this->config->getApiObjectBasketItemFieldCount() => $basketItem->getBasketItemCount(),
141
            $this->config->getApiObjectBasketItemFieldAmount() => $this->getAmountData($basketItem->getBasketItemAmount()),
142
            $this->config->getApiObjectBasketItemFieldRiskClass() => $basketItem->getBasketItemRiskClass(),
143
            $this->config->getApiObjectBasketItemFieldType() => $basketItem->getBasketItemType(),
144
        ];
145
    }
146
147
    /**
148
     * @param \Generated\Shared\Transfer\CrefoPayApiAmountTransfer $amountTransfer
149
     *
150
     * @return array
151
     */
152
    protected function convertAmountTransferToArray(CrefoPayApiAmountTransfer $amountTransfer): array
153
    {
154
        return [
155
            $this->config->getApiObjectAmountFieldAmount() => $amountTransfer->getAmount(),
156
            $this->config->getApiObjectAmountFieldVatAmount() => $amountTransfer->getVatAmount(),
157
            $this->config->getApiObjectAmountFieldVatRate() => $amountTransfer->getVatRate(),
158
        ];
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\CrefoPayApiCompanyTransfer $companyTransfer
163
     *
164
     * @return array
165
     */
166
    protected function convertCompanyTransferToArray(CrefoPayApiCompanyTransfer $companyTransfer): array
167
    {
168
        return [
169
            $this->config->getApiObjectCompanyFieldName() => $companyTransfer->getCompanyName(),
170
            $this->config->getApiObjectCompanyFieldEmail() => $companyTransfer->getEmail(),
171
            $this->config->getApiObjectCompanyFieldRegisterType() => $companyTransfer->getCompanyRegisterType(),
172
            $this->config->getApiObjectCompanyFieldRegistrationId() => $companyTransfer->getCompanyRegistrationID(),
173
            $this->config->getApiObjectCompanyFieldVatId() => $companyTransfer->getCompanyVatID(),
174
            $this->config->getApiObjectCompanyFieldTaxId() => $companyTransfer->getCompanyTaxID(),
175
        ];
176
    }
177
178
    /**
179
     * @param \Generated\Shared\Transfer\CrefoPayApiPersonTransfer $personTransfer
180
     *
181
     * @return array
182
     */
183
    protected function convertPersonTransferToArray(CrefoPayApiPersonTransfer $personTransfer): array
184
    {
185
        return [
186
            $this->config->getApiObjectPersonFieldSalutation() => $personTransfer->getSalutation(),
187
            $this->config->getApiObjectPersonFieldName() => $personTransfer->getName(),
188
            $this->config->getApiObjectPersonFieldSurname() => $personTransfer->getSurname(),
189
            $this->config->getApiObjectPersonFieldDateOfBirth() => $personTransfer->getDateOfBirth(),
190
            $this->config->getApiObjectPersonFieldEmail() => $personTransfer->getEmail(),
191
            $this->config->getApiObjectPersonFieldPhoneNumber() => $personTransfer->getPhoneNumber(),
192
            $this->config->getApiObjectPersonFieldFaxNumber() => $personTransfer->getFaxNumber(),
193
        ];
194
    }
195
196
    /**
197
     * @param \Generated\Shared\Transfer\CrefoPayApiAddressTransfer $addressTransfer
198
     *
199
     * @return array
200
     */
201
    protected function convertAddressTransferToArray(CrefoPayApiAddressTransfer $addressTransfer): array
202
    {
203
        return [
204
            $this->config->getApiObjectAddressFieldStreet() => $addressTransfer->getStreet(),
205
            $this->config->getApiObjectAddressFieldHouseNumber() => $addressTransfer->getNo(),
206
            $this->config->getApiObjectAddressFieldAdditional() => $addressTransfer->getAdditional(),
207
            $this->config->getApiObjectAddressFieldZip() => $addressTransfer->getZip(),
208
            $this->config->getApiObjectAddressFieldCity() => $addressTransfer->getCity(),
209
            $this->config->getApiObjectAddressFieldState() => $addressTransfer->getState(),
210
            $this->config->getApiObjectAddressFieldCountry() => $addressTransfer->getCountry(),
211
        ];
212
    }
213
}
214