Passed
Push — feature/eco-2295/eco-2356-main... ( 947e5b...adbe59 )
by Aleksey
01:13
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\Quote\Expander\Mapper;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\AddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AddressTransfer 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\CrefoPayApiAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiAddressTransfer 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\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...
14
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...
15
use Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...nsactionRequestTransfer 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\CrefoPayApiPersonTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiPersonTransfer 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\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...
18
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...
19
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...
20
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...
21
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...
22
use SprykerEco\Service\CrefoPay\CrefoPayServiceInterface;
23
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
24
use SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface;
25
26
class CrefoPayQuoteExpanderMapper implements CrefoPayQuoteExpanderMapperInterface
27
{
28
    protected const INTEGRATION_TYPE = 'SecureFields';
29
    protected const AUTO_CAPTURE = 'false';
30
    protected const CONTEXT = 'ONLINE';
31
    protected const USER_TYPE = 'PRIVATE';
32
    protected const SALUTATION_MAPPING = ['Mr' => 'M', 'Ms' => 'F', 'Mrs' => 'F', 'Dr' => 'M'];
33
    protected const AVAILABLE_LOCALES = ['EN', 'DE', 'ES', 'FR', 'IT', 'NL'];
34
    protected const DEFAULT_LOCALE = 'EN';
35
    protected const SHIPPING_COSTS_DESCRIPTION = 'Shipping Costs';
36
    protected const SHIPPING_COSTS_COUNT = 1;
37
38
    /**
39
     * @var \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface
40
     */
41
    protected $service;
42
43
    /**
44
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
45
     */
46
    protected $config;
47
48
    /**
49
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface
50
     */
51
    protected $localeFacade;
52
53
    /**
54
     * @param \SprykerEco\Service\CrefoPay\CrefoPayServiceInterface $service
55
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
56
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToLocaleFacadeInterface $localeFacade
57
     */
58
    public function __construct(
59
        CrefoPayServiceInterface $service,
60
        CrefoPayConfig $config,
61
        CrefoPayToLocaleFacadeInterface $localeFacade
62
    ) {
63
        $this->service = $service;
64
        $this->config = $config;
65
        $this->localeFacade = $localeFacade;
66
    }
67
68
    /**
69
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
70
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
71
     *
72
     * @return \Generated\Shared\Transfer\CrefoPayApiRequestTransfer
73
     */
74
    public function mapQuoteTransferToRequestTransfer(
75
        QuoteTransfer $quoteTransfer,
76
        CrefoPayApiRequestTransfer $requestTransfer
77
    ): CrefoPayApiRequestTransfer {
78
        $requestTransfer->setCreateTransactionRequest(
79
            $this->createCreateTransactionRequestTransfer($quoteTransfer)
80
        );
81
82
        return $requestTransfer;
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
87
     *
88
     * @return \Generated\Shared\Transfer\CrefoPayApiCreateTransactionRequestTransfer
89
     */
90
    protected function createCreateTransactionRequestTransfer(QuoteTransfer $quoteTransfer): CrefoPayApiCreateTransactionRequestTransfer
91
    {
92
        return (new CrefoPayApiCreateTransactionRequestTransfer())
93
            ->setMerchantID($this->config->getMerchantId())
94
            ->setStoreID($this->config->getStoreId())
95
            ->setOrderID($this->generateCrefoPayOrderId($quoteTransfer))
96
            ->setUserID($quoteTransfer->getCustomerReference())
97
            ->setIntegrationType(static::INTEGRATION_TYPE)
98
            ->setAutoCapture(static::AUTO_CAPTURE)
99
            ->setContext(static::CONTEXT)
100
            ->setUserType(static::USER_TYPE)
101
            ->setUserRiskClass($this->config->getUserRiskClass())
102
            ->setUserIpAddress($quoteTransfer->getCrefoPayTransaction()->getClientIp())
103
            ->setUserData($this->createCrefoPayApiPersonTransfer($quoteTransfer))
104
            ->setBillingAddress($this->getBillingAddress($quoteTransfer))
105
            ->setShippingAddress($this->getShippingAddress($quoteTransfer))
106
            ->setAmount($this->createCrefoPayApiAmountTransfer($quoteTransfer->getTotals()))
107
            ->setBasketItems($this->createBasket($quoteTransfer))
108
            ->setLocale($this->getLocale());
109
    }
110
111
    /**
112
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
113
     *
114
     * @return string
115
     */
116
    protected function generateCrefoPayOrderId(QuoteTransfer $quoteTransfer): string
117
    {
118
        return $this->service->generateCrefoPayOrderId($quoteTransfer);
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
123
     *
124
     * @return \Generated\Shared\Transfer\CrefoPayApiPersonTransfer
125
     */
126
    protected function createCrefoPayApiPersonTransfer(QuoteTransfer $quoteTransfer)
127
    {
128
        return (new CrefoPayApiPersonTransfer())
129
            ->setSalutation($this->getSalutation($quoteTransfer))
130
            ->setName($quoteTransfer->getBillingAddress()->getFirstName())
131
            ->setSurname($quoteTransfer->getBillingAddress()->getLastName())
132
            ->setDateOfBirth($quoteTransfer->getCustomer()->getDateOfBirth())
133
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
134
    }
135
136
    /**
137
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
138
     *
139
     * @return string|null
140
     */
141
    protected function getSalutation(QuoteTransfer $quoteTransfer): ?string
142
    {
143
        return static::SALUTATION_MAPPING[$quoteTransfer->getCustomer()->getSalutation()];
144
    }
145
146
    /**
147
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
148
     *
149
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
150
     */
151
    protected function getBillingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
152
    {
153
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
154
    }
155
156
    /**
157
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
158
     *
159
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
160
     */
161
    protected function getShippingAddress(QuoteTransfer $quoteTransfer): CrefoPayApiAddressTransfer
162
    {
163
        if ($quoteTransfer->getBillingSameAsShipping()) {
164
            return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getBillingAddress());
165
        }
166
167
        return $this->createCrefoPayApiAddressTransfer($quoteTransfer->getShippingAddress());
168
    }
169
170
    /**
171
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
172
     *
173
     * @return \Generated\Shared\Transfer\CrefoPayApiAddressTransfer
174
     */
175
    protected function createCrefoPayApiAddressTransfer(AddressTransfer $addressTransfer): CrefoPayApiAddressTransfer
176
    {
177
        return (new CrefoPayApiAddressTransfer())
178
            ->setStreet($addressTransfer->getAddress1())
179
            ->setNo($addressTransfer->getAddress2())
180
            ->setAdditional($addressTransfer->getAddress3())
181
            ->setZip($addressTransfer->getZipCode())
182
            ->setCity($addressTransfer->getCity())
183
            ->setCountry($addressTransfer->getIso2Code());
184
    }
185
186
    /**
187
     * @param \Generated\Shared\Transfer\TotalsTransfer $totalsTransfer
188
     *
189
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
190
     */
191
    protected function createCrefoPayApiAmountTransfer(TotalsTransfer $totalsTransfer): CrefoPayApiAmountTransfer
192
    {
193
        return (new CrefoPayApiAmountTransfer())
194
            ->setAmount($totalsTransfer->getPriceToPay())
195
            ->setVatRate($totalsTransfer->getTaxTotal()->getTaxRate())
196
            ->setVatAmount($totalsTransfer->getTaxTotal()->getAmount());
197
    }
198
199
    /**
200
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
201
     *
202
     * @return \ArrayObject
203
     */
204
    protected function createBasket(QuoteTransfer $quoteTransfer): ArrayObject
205
    {
206
        $items = array_map(
207
            function (ItemTransfer $itemTransfer) {
208
                $amount = (new CrefoPayApiAmountTransfer())
209
                    ->setAmount($itemTransfer->getSumPriceToPayAggregation())
210
                    ->setVatRate($itemTransfer->getTaxRate())
211
                    ->setVatAmount($itemTransfer->getSumTaxAmountFullAggregation());
212
213
                return (new CrefoPayApiBasketItemTransfer())
214
                    ->setBasketItemType($this->config->getProductTypeDefault())
215
                    ->setBasketItemRiskClass($this->config->getProductRiskClass())
216
                    ->setBasketItemText($itemTransfer->getName())
217
                    ->setBasketItemID($itemTransfer->getSku())
218
                    ->setBasketItemCount($itemTransfer->getQuantity())
219
                    ->setBasketItemAmount($amount);
220
            },
221
            $quoteTransfer->getItems()->getArrayCopy()
222
        );
223
224
        $basket = new ArrayObject($items);
225
        $basket->append($this->createShippingCostItem($quoteTransfer));
226
227
        return $basket;
228
    }
229
230
    /**
231
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
232
     *
233
     * @return \Generated\Shared\Transfer\CrefoPayApiBasketItemTransfer
234
     */
235
    protected function createShippingCostItem(QuoteTransfer $quoteTransfer): CrefoPayApiBasketItemTransfer
236
    {
237
        return (new CrefoPayApiBasketItemTransfer())
238
            ->setBasketItemType($this->config->getProductTypeShippingCosts())
239
            ->setBasketItemText(static::SHIPPING_COSTS_DESCRIPTION)
240
            ->setBasketItemCount(static::SHIPPING_COSTS_COUNT)
241
            ->setBasketItemAmount($this->getShippingAmount($quoteTransfer));
242
    }
243
244
    /**
245
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
246
     *
247
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer|null
248
     */
249
    protected function getShippingAmount(QuoteTransfer $quoteTransfer): ?CrefoPayApiAmountTransfer
250
    {
251
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
252
            if ($expenseTransfer->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
253
                return (new CrefoPayApiAmountTransfer())
254
                    ->setAmount($expenseTransfer->getSumPriceToPayAggregation())
255
                    ->setVatRate($expenseTransfer->getTaxRate())
256
                    ->setVatAmount($expenseTransfer->getSumTaxAmount());
257
            }
258
        }
259
260
        return null;
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    protected function getLocale(): string
267
    {
268
        if (in_array($this->localeFacade->getCurrentLocaleName(), static::AVAILABLE_LOCALES)) {
269
            return $this->localeFacade->getCurrentLocaleName();
270
        }
271
272
        return static::DEFAULT_LOCALE;
273
    }
274
}
275