Passed
Push — feature/eco-3623-payone-pay-u-... ( d4c688...8d0960 )
by Roman
04:53
created

PayuCeeSingleMapper::getArticleItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\Computop\Mapper\Init\PostPlace;
9
10
use Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...eeSinglePaymentTransfer 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\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...
12
use Spryker\Shared\Shipment\ShipmentConfig;
13
use Spryker\Yves\Router\Router\Router;
14
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
15
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
16
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
17
18
class PayuCeeSingleMapper extends AbstractMapper
19
{
20
    /**
21
     * @var string
22
     */
23
    protected const SHIPMENT_ARTICLE_NAME = 'Shipment';
24
25
    /**
26
     * @var int
27
     */
28
    protected const ONE_ITEM_AMOUNT = 1;
29
30
    /**
31
     * @var string
32
     */
33
    protected const ARTICLE_LIST_DELIMITER = ',';
34
35
    /**
36
     * @var string
37
     */
38
    protected const ROUND_ARTICLE_NAME = 'Rounded';
39
40
    /**
41
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
42
     *
43
     * @return \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer
44
     */
45
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer): ComputopPayuCeeSinglePaymentTransfer
46
    {
47
        /** @var \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer $computopPayuCeeSinglePaymentTransfer */
48
        $computopPayuCeeSinglePaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
49
50
        $encryptedMac = $this->computopApiService->generateEncryptedMac(
51
            $this->createRequestTransfer($computopPayuCeeSinglePaymentTransfer)
52
        );
53
54
        $computopPayuCeeSinglePaymentTransfer
55
            ->setMac($encryptedMac)
56
            ->setOrderDesc($this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy()))
57
            ->setCapture($this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAYU_CEE_SINGLE))
58
            ->setLanguage(mb_strtoupper($this->store->getCurrentLanguage()));
59
60
        if ($quoteTransfer->getItems()->count()) {
61
            $computopPayuCeeSinglePaymentTransfer->setArticleList(
62
                $this->getArticleList($quoteTransfer)
63
            );
64
        }
65
66
        if ($quoteTransfer->getCustomer()) {
67
            $computopPayuCeeSinglePaymentTransfer->fromArray($quoteTransfer->getCustomer()->toArray(), true);
68
        }
69
70
        return $computopPayuCeeSinglePaymentTransfer;
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
75
     *
76
     * @return \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer
77
     */
78
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer): ComputopPayuCeeSinglePaymentTransfer
79
    {
80
        $urlSuccess = $this->router->generate(ComputopRouteProviderPlugin::PAYU_CEE_SINGLE_SUCCESS, [], Router::ABSOLUTE_URL);
81
82
        return (new ComputopPayuCeeSinglePaymentTransfer())
83
            ->setTransId($this->generateTransId($quoteTransfer))
84
            ->setUrlSuccess($urlSuccess);
85
    }
86
87
    /**
88
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
89
     *
90
     * @return string
91
     */
92
    protected function getArticleList(QuoteTransfer $quoteTransfer): string
93
    {
94
        $totalSum = 0;
95
        $articlesList = [];
96
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
97
            $unitPrice = round(($itemTransfer->getSumPriceToPayAggregation() - $itemTransfer->getCanceledAmount()) / $itemTransfer->getQuantity());
98
            $itemName = $itemTransfer->getName() ? $this->replaceForbiddenCharacters($itemTransfer->getName()) : '';
99
            $totalSum += $unitPrice * $itemTransfer->getQuantity();
100
            $articlesList[] = $this->getArticleItem($itemName, $unitPrice, $itemTransfer->getQuantity());
0 ignored issues
show
Bug introduced by
$unitPrice of type double is incompatible with the type integer expected by parameter $price of SprykerEco\Yves\Computop...apper::getArticleItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
            $articlesList[] = $this->getArticleItem($itemName, /** @scrutinizer ignore-type */ $unitPrice, $itemTransfer->getQuantity());
Loading history...
101
        }
102
103
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
104
            $expanseName = $expenseTransfer->getName();
105
            if ($expenseTransfer->getType() === ShipmentConfig::SHIPMENT_EXPENSE_TYPE) {
106
                $expanseName = static::SHIPMENT_ARTICLE_NAME;
107
            }
108
109
            $expensePrice = $expenseTransfer->getSumPriceToPayAggregation() - $expenseTransfer->getCanceledAmount();
110
            $totalSum += $expensePrice;
111
            $articlesList[] = $this->getArticleItem($expanseName, $expensePrice);
112
        }
113
114
        $grandSumDifference = $quoteTransfer->getTotals()->getGrandTotal() - (int)$totalSum;
115
        if ($grandSumDifference !== 0) {
116
            $articlesList[] = $this->getArticleItem(static::ROUND_ARTICLE_NAME, $grandSumDifference);
117
        }
118
119
        return implode('+', $articlesList);
120
    }
121
122
    /**
123
     * @param string $name
124
     * @param int $price
125
     * @param int $quantity
126
     *
127
     * @return string
128
     */
129
    protected function getArticleItem(string $name, int $price, int $quantity = 1): string
130
    {
131
        return implode(static::ARTICLE_LIST_DELIMITER, [$name, $price, $quantity]);
132
    }
133
134
    /**
135
     * @param string $name
136
     *
137
     * @return string
138
     */
139
    protected function replaceForbiddenCharacters(string $name): string
140
    {
141
        return str_replace([',', '+', '-'], ' ', $name);
142
    }
143
}
144