Passed
Pull Request — feature/eco-574/eco-2266-check... (#12)
by Ruslan
06:24 queued 03:07
created

getStringDecimalExpenseNetUnitPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
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\AfterPay\Business\Payment\Mapper;
9
10
use Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tMethodsRequestTransfer 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...
11
use Generated\Shared\Transfer\AfterPayRequestAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yRequestAddressTransfer 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\AfterPayRequestCustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...RequestCustomerTransfer 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\AfterPayRequestOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...equestOrderItemTransfer 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\AfterPayRequestOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayRequestOrderTransfer 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\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer 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\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...
17
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...
18
use SprykerEco\Shared\AfterPay\AfterPayConfig;
19
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface;
20
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface;
21
22
class QuoteToRequestTransfer implements QuoteToRequestTransferInterface
23
{
24
    public const NEGATIVE_MULTIPLIER = -1;
25
    public const GIFT_CARD_PROVIDER = 'GiftCard';
26
27
    /**
28
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface
29
     */
30
    protected $moneyFacade;
31
32
    /**
33
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface
34
     */
35
    protected $storeFacade;
36
37
    /**
38
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $moneyFacade
39
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface $storeFacade
40
     */
41
    public function __construct(
42
        AfterPayToMoneyFacadeInterface $moneyFacade,
43
        AfterPayToStoreFacadeInterface $storeFacade
44
    ) {
45
        $this->moneyFacade = $moneyFacade;
46
        $this->storeFacade = $storeFacade;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
51
     *
52
     * @return \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsRequestTransfer
53
     */
54
    public function quoteToAvailablePaymentMethods(QuoteTransfer $quoteTransfer): AfterPayAvailablePaymentMethodsRequestTransfer
55
    {
56
        $requestTransfer = new AfterPayAvailablePaymentMethodsRequestTransfer();
57
58
        $requestTransfer
59
            ->setCustomer(
60
                $this->buildCustomerRequestTransfer($quoteTransfer)
61
            )
62
            ->setOrder(
63
                $this->buildOrderRequestTransfer($quoteTransfer)
64
            );
65
66
        return $requestTransfer;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
71
     *
72
     * @return \Generated\Shared\Transfer\AfterPayRequestCustomerTransfer
73
     */
74
    protected function buildCustomerRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestCustomerTransfer
75
    {
76
        $quoteBillingAddressTransfer = $quoteTransfer->getBillingAddress();
77
        $customerRequestTransfer = new AfterPayRequestCustomerTransfer();
78
79
        $customerRequestTransfer
80
            ->setFirstName($quoteBillingAddressTransfer->getFirstName())
81
            ->setLastName($quoteBillingAddressTransfer->getLastName())
82
            ->setConversationalLanguage($this->getStoreCountryIso2())
83
            ->setCustomerCategory(AfterPayConfig::API_CUSTOMER_CATEGORY_PERSON)
84
            ->setSalutation($quoteBillingAddressTransfer->getSalutation())
85
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
86
87
        $customerRequestTransfer->setAddress(
88
            $this->buildCustomerBillingAddressRequestTransfer($quoteTransfer)
89
        );
90
91
        return $customerRequestTransfer;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
96
     *
97
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
98
     */
99
    protected function buildOrderRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestOrderTransfer
100
    {
101
        $orderRequestTransfer = (new AfterPayRequestOrderTransfer())
102
            ->setTotalGrossAmount($this->getStringDecimalQuoteGrossTotal($quoteTransfer))
103
            ->setCurrency($quoteTransfer->getCurrency()->getCode());
104
105
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
106
            $orderRequestTransfer->addItem(
107
                $this->buildOrderItemRequestTransfer($itemTransfer)
108
            );
109
        }
110
111
        $orderRequestTransfer = $this->addExpensesToOrderRequestTransfer($orderRequestTransfer, $quoteTransfer);
112
113
        $this->addGiftcardItems($quoteTransfer, $orderRequestTransfer);
114
115
        return $orderRequestTransfer;
116
    }
117
118
    /**
119
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
120
     *
121
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
122
     */
123
    protected function buildOrderItemRequestTransfer(ItemTransfer $itemTransfer): AfterPayRequestOrderItemTransfer
124
    {
125
        $orderItemRequestTransfer = new AfterPayRequestOrderItemTransfer();
126
127
        $orderItemRequestTransfer
128
            ->setProductId($itemTransfer->getSku())
129
            ->setDescription($itemTransfer->getName())
130
            ->setGrossUnitPrice($this->getStringDecimalItemGrossUnitPrice($itemTransfer))
131
            ->setQuantity($itemTransfer->getQuantity())
132
            ->setVatAmount($this->getStringDecimalItemVatAmountPrice($itemTransfer))
133
            ->setVatPercent($itemTransfer->getTaxRate())
134
            ->setImageUrl($this->getImageUrlFromOrderItem($itemTransfer))
135
            ->setGroupId($itemTransfer->getGroupKey());
136
137
        return $orderItemRequestTransfer;
138
    }
139
140
    /**
141
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
142
     *
143
     * @return \Generated\Shared\Transfer\AfterPayRequestAddressTransfer
144
     */
145
    protected function buildCustomerBillingAddressRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestAddressTransfer
146
    {
147
        $customerAddressTransfer = $quoteTransfer->getBillingAddress();
148
        $customerAddressRequestTransfer = new AfterPayRequestAddressTransfer();
149
150
        $customerAddressRequestTransfer
151
            ->setCountryCode($customerAddressTransfer->getIso2Code())
152
            ->setStreet($customerAddressTransfer->getAddress1())
153
            ->setStreetNumber($customerAddressTransfer->getAddress2())
154
            ->setPostalCode($customerAddressTransfer->getZipCode())
155
            ->setPostalPlace($customerAddressTransfer->getCity());
156
157
        return $customerAddressRequestTransfer;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    protected function getStoreCountryIso2(): string
164
    {
165
        return $this->storeFacade->getCurrentStore()->getName();
166
    }
167
168
    /**
169
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
170
     *
171
     * @return string
172
     */
173
    protected function getStringDecimalQuoteGrossTotal(QuoteTransfer $quoteTransfer): string
174
    {
175
        $quoteTotal = $quoteTransfer->getTotals()->getGrandTotal();
176
        if ($quoteTransfer->getTotals()->getPriceToPay()) {
177
            $quoteTotal = $quoteTransfer->getTotals()->getPriceToPay();
178
        }
179
180
        return (string)$this->moneyFacade->convertIntegerToDecimal($quoteTotal);
181
    }
182
183
    /**
184
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
185
     *
186
     * @return string
187
     */
188
    protected function getStringDecimalQuoteNetTotal(QuoteTransfer $quoteTransfer): string
189
    {
190
        $quoteGrossTotal = $quoteTransfer->getTotals()->getGrandTotal();
191
        $quoteTaxTotal = $quoteTransfer->getTotals()->getTaxTotal()->getAmount();
192
        $quoteNetTotal = $quoteGrossTotal - $quoteTaxTotal;
193
194
        return (string)$this->moneyFacade->convertIntegerToDecimal($quoteNetTotal);
195
    }
196
197
    /**
198
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
199
     *
200
     * @return string
201
     */
202
    protected function getStringDecimalItemGrossUnitPrice(ItemTransfer $itemTransfer): string
203
    {
204
        $itemUnitGrossPrice = $itemTransfer->getUnitPriceToPayAggregation();
205
206
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitGrossPrice);
207
    }
208
209
    /**
210
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
211
     *
212
     * @return string
213
     */
214
    protected function getStringDecimalItemNetUnitPrice(ItemTransfer $itemTransfer): string
215
    {
216
        $itemUnitGrossPriceAmount = $itemTransfer->getUnitPriceToPayAggregation();
217
        $itemUnitTaxAmount = $itemTransfer->getUnitTaxAmountFullAggregation();
218
        $itemUnitNetAmount = $itemUnitGrossPriceAmount - $itemUnitTaxAmount;
219
220
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitNetAmount);
221
    }
222
223
    /**
224
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
225
     *
226
     * @return string
227
     */
228
    protected function getStringDecimalItemVatAmountPrice(ItemTransfer $itemTransfer): string
229
    {
230
        $itemVatAmountPrice = $itemTransfer->getUnitTaxAmountFullAggregation();
231
232
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemVatAmountPrice);
233
    }
234
235
    /**
236
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
237
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
238
     *
239
     * @return void
240
     */
241
    protected function addGiftcardItems(
242
        QuoteTransfer $quoteTransfer,
243
        AfterPayRequestOrderTransfer $orderRequestTransfer
244
    ): void {
245
        foreach ($this->getGiftcards($quoteTransfer) as $index => $paymentTransfer) {
246
            $orderItemRequestTransfer = new AfterPayRequestOrderItemTransfer();
247
            $amount = (string)$this->moneyFacade->convertIntegerToDecimal(static::NEGATIVE_MULTIPLIER * $paymentTransfer->getAmount());
248
249
            $orderItemRequestTransfer
250
                ->setProductId(static::GIFT_CARD_PROVIDER . $index)
251
                ->setDescription(static::GIFT_CARD_PROVIDER . $index)
252
                ->setGrossUnitPrice($amount)
253
                ->setQuantity(1);
254
255
            $orderRequestTransfer->addItem($orderItemRequestTransfer);
256
        }
257
    }
258
259
    /**
260
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
261
     *
262
     * @return \Generated\Shared\Transfer\PaymentTransfer[]
263
     */
264
    protected function getGiftcards(QuoteTransfer $quoteTransfer): array
265
    {
266
        $giftCardPayments = [];
267
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
268
            if ($paymentTransfer->getPaymentMethod() !== static::GIFT_CARD_PROVIDER) {
269
                continue;
270
            }
271
272
            $giftCardPayments[] = $paymentTransfer;
273
        }
274
275
        return $giftCardPayments;
276
    }
277
278
    /**
279
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
280
     *
281
     * @return string
282
     */
283
    protected function getImageUrlFromOrderItem(ItemTransfer $itemTransfer): string
284
    {
285
        foreach ($itemTransfer->getImages() as $imageTransfer) {
286
            $imageUrl = $imageTransfer->getExternalUrlSmall();
287
288
            if (!$this->hasImageUrlHttpProtocol($imageUrl)) {
289
                return 'http:' . $imageUrl;
290
            }
291
292
            return $imageUrl;
293
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
294
    }
295
296
    /**
297
     * @param string $url
298
     *
299
     * @return bool
300
     */
301
    protected function hasImageUrlHttpProtocol(string $url): bool
302
    {
303
        $hasImageHttpProtocolInUrl = strpos($url, 'http:') || strpos($url, 'https:');
304
305
        return $hasImageHttpProtocolInUrl !== false;
306
    }
307
308
    /**
309
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
310
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
311
     *
312
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
313
     */
314
    protected function addExpensesToOrderRequestTransfer(
315
        AfterPayRequestOrderTransfer $orderRequestTransfer,
316
        QuoteTransfer $quoteTransfer
317
    ): AfterPayRequestOrderTransfer {
318
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
319
            if ($expenseTransfer->getSumPriceToPayAggregation() > 0) {
320
                $orderRequestTransfer->addItem(
321
                    $this->buildOrderExpenseRequestTransfer($expenseTransfer)
322
                );
323
            }
324
        }
325
326
        return $orderRequestTransfer;
327
    }
328
329
    /**
330
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
331
     *
332
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
333
     */
334
    protected function buildOrderExpenseRequestTransfer(ExpenseTransfer $expenseTransfer): AfterPayRequestOrderItemTransfer
335
    {
336
        $item = (new AfterPayRequestOrderItemTransfer())
337
            ->setProductId($expenseTransfer->getType())
338
            ->setDescription($expenseTransfer->getName())
339
            ->setGrossUnitPrice($this->getStringDecimalExpenseGrossUnitPrice($expenseTransfer))
340
            ->setNetUnitPrice($this->getStringDecimalExpenseNetUnitPrice($expenseTransfer))
341
            ->setQuantity($expenseTransfer->getQuantity());
342
343
        return $item;
344
    }
345
346
    /**
347
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
348
     *
349
     * @return string
350
     */
351
    protected function getStringDecimalExpenseGrossUnitPrice(ExpenseTransfer $expenseTransfer): string
352
    {
353
        $expenseUnitGrossPrice = $expenseTransfer->getUnitPriceToPayAggregation();
354
355
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitGrossPrice);
356
    }
357
358
    /**
359
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
360
     *
361
     * @return string
362
     */
363
    protected function getStringDecimalExpenseNetUnitPrice(ExpenseTransfer $expenseTransfer): string
364
    {
365
        $expenseUnitGrossPriceAmount = $expenseTransfer->getUnitPriceToPayAggregation();
366
        $expenseUnitTaxAmount = $expenseTransfer->getUnitTaxAmount();
367
        $expenseUnitNetAmount = $expenseUnitGrossPriceAmount - $expenseUnitTaxAmount;
368
369
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitNetAmount);
370
    }
371
}
372