Passed
Push — feature/eco-2295/eco-2356-main... ( 947e5b...adbe59 )
by Aleksey
01:13
created

CrefoPayCheckoutPostSaveHookMapper::getShippingAmount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
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 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...
11
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...
12
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...
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 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...
15
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
16
17
class CrefoPayCheckoutPostSaveHookMapper implements CrefoPayCheckoutHookMapperInterface
18
{
19
    protected const GET_PAYMENT_METHOD_PATTERN = 'get%s';
20
21
    /**
22
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
23
     */
24
    protected $config;
25
26
    /**
27
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
28
     */
29
    public function __construct(CrefoPayConfig $config)
30
    {
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
36
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
37
     *
38
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
39
     */
40
    public function mapQuoteTransferToRequestTransfer(
41
        QuoteTransfer $quoteTransfer,
42
        CrefoPayApiRequestTransfer $requestTransfer
43
    ): CrefoPayApiRequestTransfer {
44
        $requestTransfer->setReserveRequest(
45
            $this->createReserveRequestTransfer($quoteTransfer)
46
        );
47
48
        return $requestTransfer;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
53
     *
54
     * @return \Generated\Shared\Transfer\CrefoPayApiReserveRequestTransfer
55
     */
56
    protected function createReserveRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiReserveRequestTransfer
57
    {
58
        return (new CrefoPayApiReserveRequestTransfer())
59
            ->setMerchantID($this->config->getMerchantId())
60
            ->setStoreID($this->config->getStoreId())
61
            ->setOrderID($quoteTransfer->getCrefoPayTransaction()->getCrefoPayOrderId())
62
            ->setPaymentMethod($this->getPaymentMethod($quoteTransfer))
63
            ->setPaymentInstrumentID($this->getPaymentInstrumentId($quoteTransfer))
64
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()));
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
69
     *
70
     * @return string|null
71
     */
72
    protected function getPaymentInstrumentId(QuoteTransfer $quoteTransfer): ?string
73
    {
74
        $method = sprintf(
75
            static::GET_PAYMENT_METHOD_PATTERN,
76
            ucfirst($quoteTransfer->getPayment()->getPaymentSelection())
77
        );
78
79
        if (!method_exists($quoteTransfer->getPayment(), $method)) {
80
            return null;
81
        }
82
83
        return $quoteTransfer->getPayment()->$method()->getPaymentInstrumentId();
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     *
89
     * @return string|null
90
     */
91
    protected function getPaymentMethod(QuoteTransfer $quoteTransfer): ?string
92
    {
93
        $method = sprintf(
94
            static::GET_PAYMENT_METHOD_PATTERN,
95
            ucfirst($quoteTransfer->getPayment()->getPaymentSelection())
96
        );
97
98
        if (!method_exists($quoteTransfer->getPayment(), $method)) {
99
            return null;
100
        }
101
102
        return $quoteTransfer->getPayment()->$method()->getPaymentMethod();
103
    }
104
105
    /**
106
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
107
     *
108
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
109
     */
110
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
111
    {
112
        return (new CrefoPayApiAmountTransfer())
113
            ->setAmount($totalsTransfer->getPriceToPay())
114
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
115
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
116
    }
117
}
118