Passed
Push — feature/eco-2300/crefopay-cred... ( 08916f...017e7a )
by Aleksey
02:33 queued 01:22
created

createShippingCostItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
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->getPaymentMethodNew($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\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
        $basket = new ArrayObject($items);
170
        $basket->append($this->createShippingCostItem($quoteTransfer));
171
172
        return $basket;
173
    }
174
175
    /**
176
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
177
     *
178
     * @return \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer
179
     */
180
    protected function createShippingCostItem(QuoteTransfer $quoteTransfer): CrefoPayApiBasketItemTransfer
181
    {
182
        return (new CrefoPayApiBasketItemTransfer())
183
            ->setBasketItemType($this->config->getProductTypeShippingCosts())
184
            ->setBasketItemText(static::SHIPPING_COSTS_DESCRIPTION)
185
            ->setBasketItemCount(static::SHIPPING_COSTS_COUNT)
186
            ->setBasketItemAmount($this->getShippingAmount($quoteTransfer));
187
    }
188
189
    /**
190
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
191
     *
192
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null
193
     */
194
    protected function getShippingAmount(QuoteTransfer $quoteTransfer): ?CrefoPayApiAmountTransfer
195
    {
196
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
197
            if ($expenseTransfer->getType() !== ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
198
                return (new CrefoPayApiAmountTransfer())
199
                    ->setAmount($expenseTransfer->getSumPriceToPayAggregation())
200
                    ->setVatRate($expenseTransfer->getTaxRate())
201
                    ->setVatAmount($expenseTransfer->getSumTaxAmount());
202
            }
203
        }
204
205
        return null;
206
    }
207
}
208