Passed
Push — feature/eco-3623-payone-pay-u-... ( e303fe...daddfa )
by Roman
04:32
created

PayuCeeSingleMapper::setCustomerData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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