Passed
Pull Request — master (#2)
by Oleksandr
12:21
created

AbstractOrderMapper::getPayload()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 76
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 76
rs 8.8072
c 0
b 0
f 0
cc 3
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Inxmail\Business\Mapper\Order;
9
10
use ArrayObject;
11
use DateTime;
12
use Generated\Shared\Transfer\InxmailRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\InxmailRequestTransfer 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\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...
14
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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\ProductAbstractTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ProductAbstractTransfer 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 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...
17
use SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToLocaleFacadeInterface;
18
use SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToMoneyFacadeInterface;
19
use SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToProductFacadeInterface;
20
use SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilDateTimeServiceInterface;
21
use SprykerEco\Zed\Inxmail\InxmailConfig;
22
23
abstract class AbstractOrderMapper implements OrderMapperInterface
24
{
25
    /**
26
     * @var \SprykerEco\Zed\Inxmail\InxmailConfig
27
     */
28
    protected $config;
29
30
    /**
31
     * @var \SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilDateTimeServiceInterface
32
     */
33
    protected $dateTimeService;
34
35
    /**
36
     * @var \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToMoneyFacadeInterface
37
     */
38
    protected $moneyFacade;
39
40
    /**
41
     * @var \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToProductFacadeInterface
42
     */
43
    protected $productFacade;
44
45
    /**
46
     * @var \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToLocaleFacadeInterface
47
     */
48
    protected $localeFacade;
49
50
    /**
51
     * @param \SprykerEco\Zed\Inxmail\InxmailConfig $config
52
     * @param \SprykerEco\Zed\Inxmail\Dependency\Service\InxmailToUtilDateTimeServiceInterface $dateTimeService
53
     * @param \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToMoneyFacadeInterface $moneyFacade
54
     * @param \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToProductFacadeInterface $productFacade
55
     * @param \SprykerEco\Zed\Inxmail\Dependency\Facade\InxmailToLocaleFacadeInterface $localeFacade
56
     */
57
    public function __construct(
58
        InxmailConfig $config,
59
        InxmailToUtilDateTimeServiceInterface $dateTimeService,
60
        InxmailToMoneyFacadeInterface $moneyFacade,
61
        InxmailToProductFacadeInterface $productFacade,
62
        InxmailToLocaleFacadeInterface $localeFacade
63
    ) {
64
        $this->config = $config;
65
        $this->dateTimeService = $dateTimeService;
66
        $this->moneyFacade = $moneyFacade;
67
        $this->productFacade = $productFacade;
68
        $this->localeFacade = $localeFacade;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
73
     *
74
     * @return \Generated\Shared\Transfer\InxmailRequestTransfer
75
     */
76
    public function map(OrderTransfer $orderTransfer): InxmailRequestTransfer
77
    {
78
        $inxmailRequestTransfer = new InxmailRequestTransfer();
79
        $inxmailRequestTransfer->setEvent($this->getEvent());
80
        $inxmailRequestTransfer->setTransactionId($orderTransfer->getOrderReference());
81
        $inxmailRequestTransfer->setPayload($this->getPayload($orderTransfer));
82
83
        return $inxmailRequestTransfer;
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
88
     *
89
     * @return array
90
     */
91
    protected function getPayload(OrderTransfer $orderTransfer): array
92
    {
93
        $locale = $orderTransfer->getCustomer()->getLocale() ?
94
            $orderTransfer->getCustomer()->getLocale()->getLocaleName() :
95
            $this->localeFacade->getCurrentLocaleName();
96
97
        $payload = [
98
            'Customer' => [
99
                'Mail' => $orderTransfer->getCustomer()->getEmail(),
100
                'Salutation' => $orderTransfer->getCustomer()->getSalutation(),
101
                'Firstname' => $orderTransfer->getCustomer()->getFirstName(),
102
                'Lastname' => $orderTransfer->getCustomer()->getLastName(),
103
                'Id' => $orderTransfer->getCustomer()->getIdCustomer(),
104
                'Language' => $locale,
105
            ],
106
            'Billing' => [
107
                'Salutation' => $orderTransfer->getBillingAddress()->getSalutation(),
108
                'Firstname' => $orderTransfer->getBillingAddress()->getFirstName(),
109
                'Lastname' => $orderTransfer->getBillingAddress()->getLastName(),
110
                'Company' => $orderTransfer->getBillingAddress()->getCompany(),
111
                'Address1' => $orderTransfer->getBillingAddress()->getAddress1(),
112
                'Address2' => $orderTransfer->getBillingAddress()->getAddress2(),
113
                'Address3' => $orderTransfer->getBillingAddress()->getAddress3(),
114
                'City' => $orderTransfer->getBillingAddress()->getCity(),
115
                'Zip' => $orderTransfer->getBillingAddress()->getZipCode(),
116
                'Country' => $orderTransfer->getBillingAddress()->getCountry()->getName(),
117
            ],
118
            'Shipping' => [
119
                'Salutation' => $orderTransfer->getShippingAddress()->getSalutation(),
120
                'Firstname' => $orderTransfer->getShippingAddress()->getFirstName(),
121
                'Lastname' => $orderTransfer->getShippingAddress()->getLastName(),
122
                'Company' => $orderTransfer->getShippingAddress()->getCompany(),
123
                'Address1' => $orderTransfer->getShippingAddress()->getAddress1(),
124
                'Address2' => $orderTransfer->getShippingAddress()->getAddress2(),
125
                'Address3' => $orderTransfer->getShippingAddress()->getAddress3(),
126
                'City' => $orderTransfer->getShippingAddress()->getCity(),
127
                'Zip' => $orderTransfer->getShippingAddress()->getZipCode(),
128
                'Country' => $orderTransfer->getShippingAddress()->getCountry()->getName(),
129
            ],
130
            'Order' => [
131
                'Number' => $orderTransfer->getIdSalesOrder(),
132
                'Comment' => $orderTransfer->getCartNote(),
133
                'CancelComment' => '',
134
                'OrderDate' => $this->dateTimeService->formatDateTime($orderTransfer->getCreatedAt()),
135
                'SubTotal' => $this->getFormattedPriceFromInt($orderTransfer->getTotals()->getSubtotal()),
136
                'Discount' => $this->getFormattedPriceFromInt($orderTransfer->getTotals()->getDiscountTotal()),
137
                'Tax' => $this->getFormattedPriceFromInt($orderTransfer->getTotals()->getTaxTotal()->getAmount()),
138
                'GrandTotal' => $this->getFormattedPriceFromInt($orderTransfer->getTotals()->getGrandTotal()),
139
                'TotalDeliveryCosts' => $this->getDeliveryCosts($orderTransfer->getExpenses()),
140
                'TotalPaymentCosts' => $this->getPaymentMethodsTotal($orderTransfer->getPayments()),
141
            ],
142
            'Payment' => $this->getPaymentMethodInfo($orderTransfer->getPayments()),
143
            'Delivery' => $this->getOrderDeliveryInfo($orderTransfer->getShipmentMethods(), $orderTransfer->getExpenses()),
144
            'Shop' => [
145
                'ShopLocale' => $this->localeFacade->getCurrentLocaleName(),
146
                'ShopUrl' => $this->config->getHostYves(),
147
            ],
148
        ];
149
150
        foreach ($orderTransfer->getItems() as $item) {
151
            $payload['OrderItem'][] = [
152
                'Name' => $item->getName(),
153
                'Sku' => $item->getSku(),
154
                'Image' => $this->getItemImageLink($item->getImages()),
155
                'DeepLink' => $this->getDeepLink($item, $locale),
156
                'Price' => $this->getFormattedPriceFromInt($item->getRefundableAmount()),
157
                'Quantity' => $item->getQuantity(),
158
                'Sum' => $this->getFormattedPriceFromInt($item->getSumPriceToPayAggregation()),
159
                'OriginalPrice' => $this->getFormattedPriceFromInt($item->getUnitPrice()),
160
                'TaxAmount' => $this->getFormattedPriceFromInt($item->getUnitTaxAmount()),
161
                'TaxRate' => $item->getTaxRate(),
162
                'Discount' => $this->getFormattedPriceFromInt($item->getUnitDiscountAmountAggregation()),
163
            ];
164
        }
165
166
        return $payload;
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    abstract protected function getEvent(): string;
173
174
    /**
175
     * @param \ArrayObject $images
176
     *
177
     * @return string
178
     */
179
    protected function getItemImageLink(ArrayObject $images): string
180
    {
181
        /**
182
         * @var \Generated\Shared\Transfer\ProductImageTransfer $image
183
         */
184
        foreach ($images as $image) {
185
            return $image->getExternalUrlSmall();
186
        }
187
188
        return '';
189
    }
190
191
    /**
192
     * @param \ArrayObject $methods
193
     * @param \ArrayObject $expenses
194
     *
195
     * @return array
196
     */
197
    protected function getOrderDeliveryInfo(ArrayObject $methods, ArrayObject $expenses): array
198
    {
199
        $result = [];
200
201
        /**
202
         * @var \Generated\Shared\Transfer\ShipmentMethodTransfer $method
203
         */
204
        foreach ($methods as $method) {
205
            $result[] = [
206
                'DeliveryMethod' => $method->getName(),
207
                'DeliveryService' => $method->getCarrierName(),
208
                'DeliveryCosts' => $this->getDeliveryCosts($expenses),
209
                'ShippingDate' => $this->dateTimeService->formatDateTime((new DateTime())::createFromFormat('U', $method->getDeliveryTime())),
0 ignored issues
show
Bug introduced by
It seems like new DateTime()::createFr...hod->getDeliveryTime()) can also be of type false; however, parameter $date of SprykerEco\Zed\Inxmail\D...rface::formatDateTime() does only seem to accept string|DateTime, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
                'ShippingDate' => $this->dateTimeService->formatDateTime(/** @scrutinizer ignore-type */ (new DateTime())::createFromFormat('U', $method->getDeliveryTime())),
Loading history...
210
                'MultiDelivery' => "No",
211
            ];
212
        }
213
214
        return $result;
215
    }
216
217
    /**
218
     * @param \ArrayObject $methods
219
     *
220
     * @return array
221
     */
222
    protected function getPaymentMethodInfo(ArrayObject $methods): array
223
    {
224
        $result = [];
225
226
        /**
227
         * @var \Generated\Shared\Transfer\PaymentTransfer $method
228
         */
229
        foreach ($methods as $method) {
230
            $result[] = [
231
                'PaymentMethod' => $method->getPaymentMethod(),
232
                'PaymentMethodCosts' => $this->getFormattedPriceFromInt($method->getAmount()),
233
                'CheckDate' => $this->dateTimeService->formatDateTime(new DateTime()),
234
            ];
235
        }
236
237
        return $result;
238
    }
239
240
    /**
241
     * @param \ArrayObject $methods
242
     *
243
     * @return string
244
     */
245
    protected function getPaymentMethodsTotal(ArrayObject $methods): string
246
    {
247
        $sum = 0;
248
249
        /**
250
         * @var \Generated\Shared\Transfer\PaymentTransfer $method
251
         */
252
        foreach ($methods as $method) {
253
            $sum += $method->getAmount();
254
        }
255
256
        return $this->getFormattedPriceFromInt($sum);
257
    }
258
259
    /**
260
     * @param int $value
261
     *
262
     * @return string
263
     */
264
    protected function getFormattedPriceFromInt(int $value): string
265
    {
266
        $moneyTransfer = $this->moneyFacade->fromInteger($value);
267
268
        return $this->moneyFacade->formatWithSymbol($moneyTransfer);
269
    }
270
271
    /**
272
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
273
     * @param string $locale
274
     *
275
     * @return string
276
     */
277
    protected function getDeepLink(ItemTransfer $itemTransfer, string $locale): string
278
    {
279
        $transfer = new ProductAbstractTransfer();
280
        $transfer->setIdProductAbstract($itemTransfer->getIdProductAbstract());
281
        $transfer->setSku($itemTransfer->getSku());
282
283
        $urls = $this->productFacade->getProductUrl($transfer)->getUrls();
284
        foreach ($urls as $url) {
285
            if ($url->getLocale() === $locale) {
286
                return $this->config->getHostYves() . $url->getUrl();
287
            }
288
        }
289
290
        return $this->config->getHostYves() . $urls->offsetGet(0)->getUrl();
291
    }
292
293
    /**
294
     * @param \ArrayObject $expenses
295
     *
296
     * @return string
297
     */
298
    protected function getDeliveryCosts(ArrayObject $expenses): string
299
    {
300
        foreach ($expenses as $expense) {
301
            if ($expense->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
302
                return $this->getFormattedPriceFromInt($expense->getSumGrossPrice());
303
            }
304
        }
305
306
        return $this->getFormattedPriceFromInt(0);
307
    }
308
}
309