Passed
Pull Request — master (#45)
by
unknown
03:36
created

PayuCeeSingleMapper::getArticleList()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 23
rs 9.4555
cc 5
nc 12
nop 1
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\Yves\Router\Router\Router;
13
use SprykerEco\Shared\Computop\ComputopConfig as ComputopSharedConfig;
14
use SprykerEco\Yves\Computop\Mapper\Init\AbstractMapper;
15
use SprykerEco\Yves\Computop\Plugin\Router\ComputopRouteProviderPlugin;
16
17
class PayuCeeSingleMapper extends AbstractMapper
18
{
19
    /**
20
     * @var string
21
     */
22
    protected const SHIPMENT_ARTICLE_NAME = 'Shipment';
23
24
    /**
25
     * @var int
26
     */
27
    protected const ONE_ITEM_AMOUNT = 1;
28
29
    /**
30
     * @var string
31
     */
32
    protected const ARTICLE_LIST_DELIMITER = ',';
33
34
    /**
35
     * @var string
36
     */
37
    protected const ROUND_ARTICLE_NAME = 'Rounded';
38
39
    /**
40
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
41
     *
42
     * @return \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer
43
     */
44
    public function createComputopPaymentTransfer(QuoteTransfer $quoteTransfer): ComputopPayuCeeSinglePaymentTransfer
45
    {
46
        /** @var \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer $computopPayuCeeSinglePaymentTransfer */
47
        $computopPayuCeeSinglePaymentTransfer = parent::createComputopPaymentTransfer($quoteTransfer);
48
49
        $encryptedMac = $this->computopApiService->generateEncryptedMac(
50
            $this->createRequestTransfer($computopPayuCeeSinglePaymentTransfer),
51
        );
52
53
        $computopPayuCeeSinglePaymentTransfer
54
            ->setMac($encryptedMac)
55
            ->setOrderDesc($this->computopApiService->getDescriptionValue($quoteTransfer->getItems()->getArrayCopy()))
56
            ->setCapture($this->getCaptureType(ComputopSharedConfig::PAYMENT_METHOD_PAYU_CEE_SINGLE))
57
            ->setLanguage(mb_strtoupper($this->store->getCurrentLanguage()));
58
59
        if ($quoteTransfer->getItems()->count()) {
60
            $computopPayuCeeSinglePaymentTransfer->setArticleList(
61
                $this->getArticleList($quoteTransfer),
62
            );
63
        }
64
65
        if ($quoteTransfer->getCustomer()) {
66
            $computopPayuCeeSinglePaymentTransfer->fromArray($quoteTransfer->getCustomer()->toArray(), true);
67
        }
68
69
        return $computopPayuCeeSinglePaymentTransfer;
70
    }
71
72
    /**
73
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
74
     *
75
     * @return \Generated\Shared\Transfer\ComputopPayuCeeSinglePaymentTransfer
76
     */
77
    protected function createTransferWithUnencryptedValues(QuoteTransfer $quoteTransfer): ComputopPayuCeeSinglePaymentTransfer
78
    {
79
        $urlSuccess = $this->router->generate(ComputopRouteProviderPlugin::PAYU_CEE_SINGLE_SUCCESS, [], Router::ABSOLUTE_URL);
80
81
        return (new ComputopPayuCeeSinglePaymentTransfer())
82
            ->setTransId($this->generateTransId($quoteTransfer))
83
            ->setUrlSuccess($urlSuccess);
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     *
89
     * @return string
90
     */
91
    protected function getArticleList(QuoteTransfer $quoteTransfer): string
92
    {
93
        $totalSum = 0;
94
        $articlesList = [];
95
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
96
            $unitPrice = (int)round(($itemTransfer->getSumPriceToPayAggregation() - $itemTransfer->getCanceledAmount()) / $itemTransfer->getQuantity());
97
            $itemName = $itemTransfer->getName() ? $this->replaceForbiddenCharacters($itemTransfer->getName()) : '';
98
            $totalSum += $unitPrice * $itemTransfer->getQuantity();
99
            $articlesList[] = $this->getArticleItem($itemName, $unitPrice, $itemTransfer->getQuantity());
100
        }
101
102
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
103
            $expensePrice = (int)($expenseTransfer->getSumPriceToPayAggregation() - $expenseTransfer->getCanceledAmount());
104
            $totalSum += $expensePrice;
105
            $articlesList[] = $this->getArticleItem($expenseTransfer->getName(), $expensePrice);
106
        }
107
108
        $grandSumDifference = (int)$quoteTransfer->getTotals()->getGrandTotal() - (int)$totalSum;
109
        if ($grandSumDifference !== 0) {
110
            $articlesList[] = $this->getArticleItem(static::ROUND_ARTICLE_NAME, $grandSumDifference);
111
        }
112
113
        return implode('+', $articlesList);
114
    }
115
116
    /**
117
     * @param string $name
118
     * @param int $price
119
     * @param int $quantity
120
     *
121
     * @return string
122
     */
123
    protected function getArticleItem(string $name, int $price, int $quantity = 1): string
124
    {
125
        return implode(static::ARTICLE_LIST_DELIMITER, [$name, $price, $quantity]);
126
    }
127
128
    /**
129
     * @param string $name
130
     *
131
     * @return string
132
     */
133
    protected function replaceForbiddenCharacters(string $name): string
134
    {
135
        return str_replace([',', '+', '-'], ' ', $name);
136
    }
137
}
138