CrefoPayCheckoutPostSaveHookMapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 3
b 0
f 0
dl 0
loc 102
rs 10

6 Methods

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