Passed
Push — feature/eco-2295/eco-2356-main... ( 8a87d8...4fb803 )
by Aleksey
01:47
created

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 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 Spryker\Shared\Shipment\ShipmentConstants;
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\Shipment\ShipmentConstants 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...
19
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
20
21
class CrefoPayCheckoutPostSaveHookMapper implements CrefoPayCheckoutHookMapperInterface
22
{
23
    protected const GET_PAYMENT_METHOD_PATTERN = 'get%s';
24
    protected const SHIPPING_COSTS_DESCRIPTION = 'Shipping Costs';
25
    protected const SHIPPING_COSTS_COUNT = 1;
26
27
    /**
28
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
29
     */
30
    protected $config;
31
32
    /**
33
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
34
     */
35
    public function __construct(CrefoPayConfig $config)
36
    {
37
        $this->config = $config;
38
    }
39
40
    /**
41
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
42
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
43
     *
44
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
45
     */
46
    public function mapQuoteTransferToRequestTransfer(
47
        QuoteTransfer $quoteTransfer,
48
        CrefoPayApiRequestTransfer $requestTransfer
49
    ): CrefoPayApiRequestTransfer {
50
        $requestTransfer->setReserveRequest(
51
            $this->createReserveRequestTransfer($quoteTransfer)
52
        );
53
54
        return $requestTransfer;
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
59
     *
60
     * @return \Generated\Shared\Transfer\CrefoPayApiReserveRequestTransfer
61
     */
62
    protected function createReserveRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiReserveRequestTransfer
63
    {
64
        return (new CrefoPayApiReserveRequestTransfer())
65
            ->setMerchantID($this->config->getMerchantId())
66
            ->setStoreID($this->config->getStoreId())
67
            ->setOrderID($quoteTransfer->getCrefoPayTransaction()->getCrefoPayOrderId())
68
            ->setPaymentMethod($this->getPaymentMethod($quoteTransfer))
69
            ->setPaymentInstrumentID($this->getPaymentInstrumentId($quoteTransfer))
70
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()))
71
            ->setBasketItems($this->createBasket($quoteTransfer));
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
76
     *
77
     * @return string|null
78
     */
79
    protected function getPaymentInstrumentId(QuoteTransfer $quoteTransfer): ?string
80
    {
81
        $method = sprintf(
82
            static::GET_PAYMENT_METHOD_PATTERN,
83
            ucfirst($quoteTransfer->getPayment()->getPaymentSelection())
84
        );
85
86
        if (!method_exists($quoteTransfer->getPayment(), $method)) {
87
            return null;
88
        }
89
90
        return $quoteTransfer->getPayment()->$method()->getPaymentInstrumentId();
91
    }
92
93
    /**
94
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
95
     *
96
     * @return string|null
97
     */
98
    protected function getPaymentMethod(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()->getPaymentMethod();
110
    }
111
112
    /**
113
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
114
     *
115
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
116
     */
117
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
118
    {
119
        return (new CrefoPayApiAmountTransfer())
120
            ->setAmount($totalsTransfer->getPriceToPay())
121
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
122
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
123
    }
124
125
    /**
126
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
127
     *
128
     * @return \ArrayObject
129
     */
130
    protected function createBasket(QuoteTransfer $quoteTransfer): ArrayObject
131
    {
132
        $items = array_map(
133
            function (ItemTransfer $itemTransfer) {
134
                $amount = (new CrefoPayApiAmountTransfer())
135
                    ->setAmount($itemTransfer->getSumPriceToPayAggregation())
136
                    ->setVatRate($itemTransfer->getTaxRate())
137
                    ->setVatAmount($itemTransfer->getSumTaxAmountFullAggregation());
138
139
                return (new CrefoPayApiBasketItemTransfer())
140
                    ->setBasketItemType($this->config->getProductTypeDefault())
141
                    ->setBasketItemRiskClass($this->config->getProductRiskClass())
142
                    ->setBasketItemText($itemTransfer->getName())
143
                    ->setBasketItemID($itemTransfer->getSku())
144
                    ->setBasketItemCount($itemTransfer->getQuantity())
145
                    ->setBasketItemAmount($amount);
146
            },
147
            $quoteTransfer->getItems()->getArrayCopy()
148
        );
149
150
        $basket = new ArrayObject($items);
151
        $basket->append($this->createShippingCostItem($quoteTransfer));
152
153
        return $basket;
154
    }
155
156
    /**
157
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
158
     *
159
     * @return \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer
160
     */
161
    protected function createShippingCostItem(QuoteTransfer $quoteTransfer): CrefoPayApiBasketItemTransfer
162
    {
163
        return (new CrefoPayApiBasketItemTransfer())
164
            ->setBasketItemType($this->config->getProductTypeShippingCosts())
165
            ->setBasketItemText(static::SHIPPING_COSTS_DESCRIPTION)
166
            ->setBasketItemCount(static::SHIPPING_COSTS_COUNT)
167
            ->setBasketItemAmount($this->getShippingAmount($quoteTransfer));
168
    }
169
170
    /**
171
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
172
     *
173
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null
174
     */
175
    protected function getShippingAmount(QuoteTransfer $quoteTransfer): ?CrefoPayApiAmountTransfer
176
    {
177
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
178
            if ($expenseTransfer->getType() !== ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
179
                return (new CrefoPayApiAmountTransfer())
180
                    ->setAmount($expenseTransfer->getSumPriceToPayAggregation())
181
                    ->setVatRate($expenseTransfer->getTaxRate())
182
                    ->setVatAmount($expenseTransfer->getSumTaxAmount());
183
            }
184
        }
185
186
        return null;
187
    }
188
}
189