Passed
Push — master ( ef0fdb...1d76f0 )
by
unknown
03:02
created

CrefoPayQuoteExpanderMapper::createGuestUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
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\CrefoPay\Business\Quote\Expander\Mapper;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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\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...
13
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...
14
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...
15
use Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...nsactionRequestTransfer 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\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...
17
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...
18
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer 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...
19
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
20
use Generated\Shared\Transfer\TotalsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\TotalsTransfer 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...
21
use SprykerEco\Service\CrefoPay\CrefoPayServiceInterface;
22
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
23
use SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface;
24
use SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface;
25
26
class CrefoPayQuoteExpanderMapper implements CrefoPayQuoteExpanderMapperInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected const INTEGRATION_TYPE = 'SecureFields';
32
33
    /**
34
     * @var string
35
     */
36
    protected const AUTO_CAPTURE = 'false';
37
38
    /**
39
     * @var string
40
     */
41
    protected const CONTEXT = 'ONLINE';
42
43
    /**
44
     * @var string
45
     */
46
    protected const USER_TYPE_PRIVATE = 'PRIVATE';
47
48
    /**
49
     * @var string
50
     */
51
    protected const USER_TYPE_BUSINESS = 'BUSINESS';
52
53
    /**
54
     * @var array
55
     */
56
    protected const AVAILABLE_LOCALES = ['EN', 'DE', 'ES', 'FR', 'IT', 'NL'];
57
58
    /**
59
     * @var string
60
     */
61
    protected const DEFAULT_LOCALE = 'EN';
62
63
    /**
64
     * @var string
65
     */
66
    protected const SHIPPING_COSTS_DESCRIPTION = 'Shipping Costs';
67
68
    /**
69
     * @var int
70
     */
71
    protected const SHIPPING_COSTS_COUNT = 1;
72
73
    /**
74
     * @uses \Spryker\Shared\Shipment\ShipmentConfig::SHIPMENT_EXPENSE_TYPE
75
     *
76
     * @var string
77
     */
78
    protected const SHIPMENT_EXPENSE_TYPE = 'SHIPMENT_EXPENSE_TYPE';
79
80
    /**
81
     * @var \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface
82
     */
83
    protected CrefoPayServiceInterface $crefoPayService;
84
85
    /**
86
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface
87
     */
88
    protected CrefoPayToUtilTextServiceInterface $utilTextService;
89
90
    /**
91
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
92
     */
93
    protected CrefoPayConfig $config;
94
95
    /**
96
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface
97
     */
98
    protected $localeFacade;
99
100
    /**
101
     * @param \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface $crefoPayService
102
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface $utilTextService
103
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
104
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface $localeFacade
105
     */
106
    public function __construct(
107
        CrefoPayServiceInterface $crefoPayService,
108
        CrefoPayToUtilTextServiceInterface $utilTextService,
109
        CrefoPayConfig $config,
110
        CrefoPayToLocaleFacadeInterface $localeFacade
111
    ) {
112
        $this->crefoPayService = $crefoPayService;
113
        $this->utilTextService = $utilTextService;
114
        $this->config = $config;
115
        $this->localeFacade = $localeFacade;
116
    }
117
118
    /**
119
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
120
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
121
     *
122
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
123
     */
124
    public function mapQuoteTransferToRequestTransfer(
125
        QuoteTransfer $quoteTransfer,
126
        CrefoPayApiRequestTransfer $requestTransfer
127
    ): CrefoPayApiRequestTransfer {
128
        $requestTransfer->setCreateTransactionRequest(
129
            $this->createCreateTransactionRequestTransfer($quoteTransfer),
130
        );
131
132
        return $requestTransfer;
133
    }
134
135
    /**
136
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
137
     *
138
     * @return \Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer
139
     */
140
    protected function createCreateTransactionRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiCreateTransactionRequestTransfer
141
    {
142
        $createTransactionRequestTransfer = (new CrefoPayApiCreateTransactionRequestTransfer())
143
            ->setMerchantID($this->config->getMerchantId())
144
            ->setStoreID($this->config->getStoreId())
145
            ->setOrderID($this->crefoPayService->generateCrefoPayOrderId($quoteTransfer))
146
            ->setUserID($this->crefoPayService->generateCrefoPayUserId($quoteTransfer))
147
            ->setIntegrationType(static::INTEGRATION_TYPE)
148
            ->setAutoCapture(static::AUTO_CAPTURE)
149
            ->setContext(static::CONTEXT)
150
            ->setUserType($this->getUserType())
151
            ->setUserRiskClass($this->config->getUserRiskClass())
152
            ->setUserIpAddress($quoteTransfer->getCrefoPayTransaction()->getClientIp())
153
            ->setBillingAddress($this->getBillingAddress($quoteTransfer))
154
            ->setShippingAddress($this->getShippingAddress($quoteTransfer))
155
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()))
156
            ->setBasketItems($this->createBasket($quoteTransfer))
157
            ->setLocale($this->getLocale());
158
159
        return $this->addUserInformation($quoteTransfer, $createTransactionRequestTransfer);
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    protected function getUserType(): string
166
    {
167
        if ($this->config->getIsBusinessToBusiness()) {
168
            return static::USER_TYPE_BUSINESS;
169
        }
170
171
        return static::USER_TYPE_PRIVATE;
172
    }
173
174
    /**
175
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
176
     * @param \Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer $createTransactionRequestTransfer
177
     *
178
     * @return \Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer
179
     */
180
    protected function addUserInformation(
181
        QuoteTransfer $quoteTransfer,
182
        CrefoPayApiCreateTransactionRequestTransfer $createTransactionRequestTransfer
183
    ): CrefoPayApiCreateTransactionRequestTransfer {
184
        if ($this->config->getIsBusinessToBusiness()) {
185
            $createTransactionRequestTransfer->setCompanyData(
186
                $quoteTransfer->getCrefoPayCompany(),
187
            );
188
189
            return $createTransactionRequestTransfer;
190
        }
191
192
        $createTransactionRequestTransfer->setUserData(
193
            $this->createCrefoPayApiPersonTransfer($quoteTransfer),
194
        );
195
196
        return $createTransactionRequestTransfer;
197
    }
198
199
    /**
200
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
201
     *
202
     * @return \Generated\Shared\Transfer\CrefoPayApiPersonTransfer
203
     */
204
    protected function createCrefoPayApiPersonTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiPersonTransfer
205
    {
206
        return (new CrefoPayApiPersonTransfer())
207
            ->setSalutation($this->getSalutation($quoteTransfer))
208
            ->setName($quoteTransfer->getBillingAddress()->getFirstName())
209
            ->setSurname($quoteTransfer->getBillingAddress()->getLastName())
210
            ->setDateOfBirth($quoteTransfer->getCustomer()->getDateOfBirth())
211
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
212
    }
213
214
    /**
215
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
216
     *
217
     * @return string|null
218
     */
219
    protected function getSalutation(QuoteTransfer $quoteTransfer): ?string
220
    {
221
        $salutationMap = $this->config->getCrefoPaySalutationMap();
222
        if (array_key_exists($quoteTransfer->getCustomerOrFail()->getSalutation(), $salutationMap)) {
223
            return $salutationMap[$quoteTransfer->getCustomerOrFail()->getSalutation()];
224
        }
225
226
        return null;
227
    }
228
229
    /**
230
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
231
     *
232
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
233
     */
234
    protected function getBillingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
235
    {
236
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
237
    }
238
239
    /**
240
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
241
     *
242
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
243
     */
244
    protected function getShippingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
245
    {
246
        if ($quoteTransfer->getBillingSameAsShipping()) {
247
            return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
248
        }
249
250
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getShippingAddress());
251
    }
252
253
    /**
254
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
255
     *
256
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
257
     */
258
    protected function createCrefoPayApiAddressTransfer(AddressTransfer $addressTransfer): CrefoPayApiAddressTransfer
259
    {
260
        return (new CrefoPayApiAddressTransfer())
261
            ->setStreet($addressTransfer->getAddress1())
262
            ->setNo($addressTransfer->getAddress2())
263
            ->setAdditional($addressTransfer->getAddress3())
264
            ->setZip($addressTransfer->getZipCode())
265
            ->setCity($addressTransfer->getCity())
266
            ->setCountry($addressTransfer->getIso2Code());
267
    }
268
269
    /**
270
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
271
     *
272
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
273
     */
274
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
275
    {
276
        return (new CrefoPayApiAmountTransfer())
277
            ->setAmount($totalsTransfer->getPriceToPay())
278
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
279
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
280
    }
281
282
    /**
283
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
284
     *
285
     * @return \ArrayObject
286
     */
287
    protected function createBasket(QuoteTransfer $quoteTransfer): ArrayObject
288
    {
289
        $items = array_map(
290
            function (ItemTransfer $itemTransfer) {
291
                $amount = (new CrefoPayApiAmountTransfer())
292
                    ->setAmount($itemTransfer->getSumPriceToPayAggregation())
293
                    ->setVatRate($itemTransfer->getTaxRate())
294
                    ->setVatAmount($itemTransfer->getSumTaxAmountFullAggregation());
295
296
                return (new CrefoPayApiBasketItemTransfer())
297
                    ->setBasketItemType($this->config->getProductTypeDefault())
298
                    ->setBasketItemRiskClass($this->config->getProductRiskClass())
299
                    ->setBasketItemText($itemTransfer->getName())
300
                    ->setBasketItemID($this->crefoPayService->generateCrefoPayBasketItemId($itemTransfer))
301
                    ->setBasketItemCount($itemTransfer->getQuantity())
302
                    ->setBasketItemAmount($amount);
303
            },
304
            $quoteTransfer->getItems()->getArrayCopy(),
305
        );
306
307
        $basket = new ArrayObject(array_values($items));
308
        $basket->append($this->createShippingCostItem($quoteTransfer));
309
310
        return $basket;
311
    }
312
313
    /**
314
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
315
     *
316
     * @return \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer
317
     */
318
    protected function createShippingCostItem(QuoteTransfer $quoteTransfer): CrefoPayApiBasketItemTransfer
319
    {
320
        return (new CrefoPayApiBasketItemTransfer())
321
            ->setBasketItemType($this->config->getProductTypeShippingCosts())
322
            ->setBasketItemText(static::SHIPPING_COSTS_DESCRIPTION)
323
            ->setBasketItemCount(static::SHIPPING_COSTS_COUNT)
324
            ->setBasketItemAmount($this->getShippingAmount($quoteTransfer));
325
    }
326
327
    /**
328
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
329
     *
330
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null
331
     */
332
    protected function getShippingAmount(QuoteTransfer $quoteTransfer): ?CrefoPayApiAmountTransfer
333
    {
334
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
335
            if ($expenseTransfer->getType() === static::SHIPMENT_EXPENSE_TYPE) {
336
                return (new CrefoPayApiAmountTransfer())
337
                    ->setAmount($expenseTransfer->getSumPriceToPayAggregation())
338
                    ->setVatRate($expenseTransfer->getTaxRate())
339
                    ->setVatAmount($expenseTransfer->getSumTaxAmount());
340
            }
341
        }
342
343
        return null;
344
    }
345
346
    /**
347
     * @return string
348
     */
349
    protected function getLocale(): string
350
    {
351
        $locale = strtoupper(substr($this->localeFacade->getCurrentLocaleName(), 0, 2));
352
        if (in_array($locale, static::AVAILABLE_LOCALES)) {
353
            return $locale;
354
        }
355
356
        return static::DEFAULT_LOCALE;
357
    }
358
}
359