Completed
Push — master ( eaf845...272b76 )
by Oleksandr
13s queued 10s
created

CrefoPayQuoteExpanderMapper   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 119
c 5
b 1
f 0
dl 0
loc 340
rs 10
wmc 28

19 Methods

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