Passed
Push — feature/eco-2295/eco-2356-main... ( b4f498...c64b87 )
by Aleksey
12:46 queued 10s
created

CrefoPayQuoteExpanderMapper::getUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
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 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';
33
    protected const SALUTATION_MAPPING = ['Mr' => 'M', 'Ms' => 'F', 'Mrs' => 'F', 'Dr' => 'M'];
34
    protected const AVAILABLE_LOCALES = ['EN', 'DE', 'ES', 'FR', 'IT', 'NL'];
35
    protected const DEFAULT_LOCALE = 'EN';
36
    protected const SHIPPING_COSTS_DESCRIPTION = 'Shipping Costs';
37
    protected const SHIPPING_COSTS_COUNT = 1;
38
    protected const GUEST_USER_ID_LENGTH = 6;
39
40
    /**
41
     * @var \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface
42
     */
43
    protected $crefoPayService;
44
45
    /**
46
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface
47
     */
48
    protected $utilTextService;
49
50
    /**
51
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
52
     */
53
    protected $config;
54
55
    /**
56
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface
57
     */
58
    protected $localeFacade;
59
60
    /**
61
     * @param \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface $crefoPayService
62
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface $utilTextService
63
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
64
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface $localeFacade
65
     */
66
    public function __construct(
67
        CrefoPayServiceInterface $crefoPayService,
68
        CrefoPayToUtilTextServiceInterface $utilTextService,
69
        CrefoPayConfig $config,
70
        CrefoPayToLocaleFacadeInterface $localeFacade
71
    ) {
72
        $this->crefoPayService = $crefoPayService;
73
        $this->utilTextService = $utilTextService;
74
        $this->config = $config;
75
        $this->localeFacade = $localeFacade;
76
    }
77
78
    /**
79
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
80
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
81
     *
82
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
83
     */
84
    public function mapQuoteTransferToRequestTransfer(
85
        QuoteTransfer $quoteTransfer,
86
        CrefoPayApiRequestTransfer $requestTransfer
87
    ): CrefoPayApiRequestTransfer {
88
        $requestTransfer->setCreateTransactionRequest(
89
            $this->createCreateTransactionRequestTransfer($quoteTransfer)
90
        );
91
92
        return $requestTransfer;
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
97
     *
98
     * @return \Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer
99
     */
100
    protected function createCreateTransactionRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiCreateTransactionRequestTransfer
101
    {
102
        return (new CrefoPayApiCreateTransactionRequestTransfer())
103
            ->setMerchantID($this->config->getMerchantId())
104
            ->setStoreID($this->config->getStoreId())
105
            ->setOrderID($this->generateCrefoPayOrderId($quoteTransfer))
106
            ->setUserID($this->getUserId($quoteTransfer))
107
            ->setIntegrationType(static::INTEGRATION_TYPE)
108
            ->setAutoCapture(static::AUTO_CAPTURE)
109
            ->setContext(static::CONTEXT)
110
            ->setUserType(static::USER_TYPE)
111
            ->setUserRiskClass($this->config->getUserRiskClass())
112
            ->setUserIpAddress($quoteTransfer->getCrefoPayTransaction()->getClientIp())
113
            ->setUserData($this->createCrefoPayApiPersonTransfer($quoteTransfer))
114
            ->setBillingAddress($this->getBillingAddress($quoteTransfer))
115
            ->setShippingAddress($this->getShippingAddress($quoteTransfer))
116
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()))
117
            ->setBasketItems($this->createBasket($quoteTransfer))
118
            ->setLocale($this->getLocale());
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
123
     *
124
     * @return string
125
     */
126
    protected function generateCrefoPayOrderId(QuoteTransfer $quoteTransfer): string
127
    {
128
        return $this->crefoPayService->generateCrefoPayOrderId($quoteTransfer);
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
133
     *
134
     * @return string
135
     */
136
    protected function getUserId(QuoteTransfer $quoteTransfer): string
137
    {
138
        if ($quoteTransfer->getCustomer()->getIsGuest()) {
139
            return $this->utilTextService->generateRandomString(static::GUEST_USER_ID_LENGTH);
140
        }
141
142
        return $quoteTransfer->getCustomerReference();
143
    }
144
145
    /**
146
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
147
     *
148
     * @return \Generated\Shared\Transfer\CrefoPayApiPersonTransfer
149
     */
150
    protected function createCrefoPayApiPersonTransfer(QuoteTransfer $quoteTransfer)
151
    {
152
        return (new CrefoPayApiPersonTransfer())
153
            ->setSalutation($this->getSalutation($quoteTransfer))
154
            ->setName($quoteTransfer->getBillingAddress()->getFirstName())
155
            ->setSurname($quoteTransfer->getBillingAddress()->getLastName())
156
            ->setDateOfBirth($quoteTransfer->getCustomer()->getDateOfBirth())
157
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
158
    }
159
160
    /**
161
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
162
     *
163
     * @return string|null
164
     */
165
    protected function getSalutation(QuoteTransfer $quoteTransfer): ?string
166
    {
167
        return static::SALUTATION_MAPPING[$quoteTransfer->getCustomer()->getSalutation()];
168
    }
169
170
    /**
171
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
172
     *
173
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
174
     */
175
    protected function getBillingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
176
    {
177
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
178
    }
179
180
    /**
181
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
182
     *
183
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
184
     */
185
    protected function getShippingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
186
    {
187
        if ($quoteTransfer->getBillingSameAsShipping()) {
188
            return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
189
        }
190
191
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getShippingAddress());
192
    }
193
194
    /**
195
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
196
     *
197
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
198
     */
199
    protected function createCrefoPayApiAddressTransfer(AddressTransfer $addressTransfer): CrefoPayApiAddressTransfer
200
    {
201
        return (new CrefoPayApiAddressTransfer())
202
            ->setStreet($addressTransfer->getAddress1())
203
            ->setNo($addressTransfer->getAddress2())
204
            ->setAdditional($addressTransfer->getAddress3())
205
            ->setZip($addressTransfer->getZipCode())
206
            ->setCity($addressTransfer->getCity())
207
            ->setCountry($addressTransfer->getIso2Code());
208
    }
209
210
    /**
211
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
212
     *
213
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
214
     */
215
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
216
    {
217
        return (new CrefoPayApiAmountTransfer())
218
            ->setAmount($totalsTransfer->getPriceToPay())
219
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
220
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
221
    }
222
223
    /**
224
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
225
     *
226
     * @return \ArrayObject
227
     */
228
    protected function createBasket(QuoteTransfer $quoteTransfer): ArrayObject
229
    {
230
        $items = array_map(
231
            function (ItemTransfer $itemTransfer) {
232
                $amount = (new CrefoPayApiAmountTransfer())
233
                    ->setAmount($itemTransfer->getSumPriceToPayAggregation())
234
                    ->setVatRate($itemTransfer->getTaxRate())
235
                    ->setVatAmount($itemTransfer->getSumTaxAmountFullAggregation());
236
237
                return (new CrefoPayApiBasketItemTransfer())
238
                    ->setBasketItemType($this->config->getProductTypeDefault())
239
                    ->setBasketItemRiskClass($this->config->getProductRiskClass())
240
                    ->setBasketItemText($itemTransfer->getName())
241
                    ->setBasketItemID($itemTransfer->getSku())
242
                    ->setBasketItemCount($itemTransfer->getQuantity())
243
                    ->setBasketItemAmount($amount);
244
            },
245
            $quoteTransfer->getItems()->getArrayCopy()
246
        );
247
248
        $basket = new ArrayObject($items);
249
        $basket->append($this->createShippingCostItem($quoteTransfer));
250
251
        return $basket;
252
    }
253
254
    /**
255
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
256
     *
257
     * @return \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer
258
     */
259
    protected function createShippingCostItem(QuoteTransfer $quoteTransfer): CrefoPayApiBasketItemTransfer
260
    {
261
        return (new CrefoPayApiBasketItemTransfer())
262
            ->setBasketItemType($this->config->getProductTypeShippingCosts())
263
            ->setBasketItemText(static::SHIPPING_COSTS_DESCRIPTION)
264
            ->setBasketItemCount(static::SHIPPING_COSTS_COUNT)
265
            ->setBasketItemAmount($this->getShippingAmount($quoteTransfer));
266
    }
267
268
    /**
269
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
270
     *
271
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null
272
     */
273
    protected function getShippingAmount(QuoteTransfer $quoteTransfer): ?CrefoPayApiAmountTransfer
274
    {
275
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
276
            if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
277
                return (new CrefoPayApiAmountTransfer())
278
                    ->setAmount($expenseTransfer->getSumPriceToPayAggregation())
279
                    ->setVatRate($expenseTransfer->getTaxRate())
280
                    ->setVatAmount($expenseTransfer->getSumTaxAmount());
281
            }
282
        }
283
284
        return null;
285
    }
286
287
    /**
288
     * @return string
289
     */
290
    protected function getLocale(): string
291
    {
292
        if (in_array($this->localeFacade->getCurrentLocaleName(), static::AVAILABLE_LOCALES)) {
293
            return $this->localeFacade->getCurrentLocaleName();
294
        }
295
296
        return static::DEFAULT_LOCALE;
297
    }
298
}
299