Passed
Push — feature/eco-2300/crefopay-cred... ( 4beef5...ee9c93 )
by Aleksey
01:18
created

getPaymentMethodNew()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
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\Hook\Checkout\Mapper;
9
10
use ArrayObject;
11
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...
12
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...
13
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...
14
use Generated\Shared\Transfer\CrefoPayApiReserveRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iReserveRequestTransfer 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\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...
16
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...
17
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...
18
use SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface;
19
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
20
21
class CrefoPayCheckoutPostSaveHookMapper implements CrefoPayCheckoutHookMapperInterface
22
{
23
    protected const GET_PAYMENT_METHOD_PATTERN = 'get%s';
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface
27
     */
28
    protected $paymentMethodMapper;
29
30
    /**
31
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
32
     */
33
    protected $config;
34
35
    /**
36
     * @param \SprykerEco\Zed\CrefoPay\Business\Mapper\PaymentMethod\CrefoPayPaymentMethodMapperInterface $paymentMethodMapper
37
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
38
     */
39
    public function __construct(
40
        CrefoPayPaymentMethodMapperInterface $paymentMethodMapper,
41
        CrefoPayConfig $config
42
    ) {
43
        $this->paymentMethodMapper = $paymentMethodMapper;
44
        $this->config = $config;
45
    }
46
47
    /**
48
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
49
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
50
     *
51
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
52
     */
53
    public function mapQuoteTransferToRequestTransfer(
54
        QuoteTransfer $quoteTransfer,
55
        CrefoPayApiRequestTransfer $requestTransfer
56
    ): CrefoPayApiRequestTransfer {
57
        $requestTransfer->setReserveRequest(
58
            $this->createReserveRequestTransfer($quoteTransfer)
59
        );
60
61
        return $requestTransfer;
62
    }
63
64
    /**
65
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
66
     *
67
     * @return \Generated\Shared\Transfer\CrefoPayApiReserveRequestTransfer
68
     */
69
    protected function createReserveRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiReserveRequestTransfer
70
    {
71
        return (new CrefoPayApiReserveRequestTransfer())
72
            ->setMerchantID($this->config->getMerchantId())
73
            ->setStoreID($this->config->getStoreId())
74
            ->setOrderID($quoteTransfer->getCrefoPayTransaction()->getCrefoPayOrderId())
75
            ->setPaymentMethod($this->getPaymentMethodNew($quoteTransfer))
76
            ->setPaymentInstrumentID($this->getPaymentInstrumentId($quoteTransfer))
77
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()))
78
            ->setBasketItems($this->createBasket($quoteTransfer));
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
83
     *
84
     * @return string|null
85
     */
86
    protected function getPaymentMethod(QuoteTransfer $quoteTransfer): ?string
87
    {
88
        return $this->paymentMethodMapper->mapInternalToExternalPaymentMethodName(
89
            $quoteTransfer->getPayment()->getPaymentSelection()
90
        );
91
    }
92
93
    /**
94
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
95
     *
96
     * @return string|null
97
     */
98
    protected function getPaymentInstrumentId(QuoteTransfer $quoteTransfer): ?string
99
    {
100
        $method = sprintf(
101
            static::GET_PAYMENT_METHOD_PATTERN,
102
            ucfirst($quoteTransfer->getPayment()->getPaymentSelection())
103
        );
104
105
        if (!method_exists($quoteTransfer->getPayment(), $method)) {
106
            return null;
107
        }
108
109
        return $quoteTransfer->getPayment()->$method()->getPaymentInstrumentId();
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
114
     *
115
     * @return string|null
116
     */
117
    protected function getPaymentMethodNew(QuoteTransfer $quoteTransfer): ?string
118
    {
119
        $method = sprintf(
120
            static::GET_PAYMENT_METHOD_PATTERN,
121
            ucfirst($quoteTransfer->getPayment()->getPaymentSelection())
122
        );
123
124
        if (!method_exists($quoteTransfer->getPayment(), $method)) {
125
            return null;
126
        }
127
128
        return $quoteTransfer->getPayment()->$method()->getPaymentMethod();
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
133
     *
134
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
135
     */
136
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
137
    {
138
        return (new CrefoPayApiAmountTransfer())
139
            ->setAmount($totalsTransfer->getPriceToPay())
140
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
141
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
146
     *
147
     * @return \ArrayObject
148
     */
149
    protected function createBasket(QuoteTransfer $quoteTransfer): ArrayObject
150
    {
151
        $items = array_map(
152
            function (ItemTransfer $itemTransfer) {
153
                $amount = (new CrefoPayApiAmountTransfer())
154
                    ->setAmount($itemTransfer->getSumPriceToPayAggregation())
155
                    ->setVatRate($itemTransfer->getTaxRate())
156
                    ->setVatAmount($itemTransfer->getSumTaxAmountFullAggregation());
157
158
                return (new CrefoPayApiBasketItemTransfer())
159
                    ->setBasketItemType($this->config->getProductTypeDefault())
160
                    ->setBasketItemRiskClass($this->config->getProductRiskClass())
161
                    ->setBasketItemText($itemTransfer->getName())
162
                    ->setBasketItemID($itemTransfer->getSku())
163
                    ->setBasketItemCount($itemTransfer->getQuantity())
164
                    ->setBasketItemAmount($amount);
165
            },
166
            $quoteTransfer->getItems()->getArrayCopy()
167
        );
168
169
        return new ArrayObject($items);
170
    }
171
}
172