Passed
Push — feature/eco-3742/add-payment-j... ( 290191...0112cf )
by Stanislav
26:17 queued 21:19
created

ReservationRequestConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
eloc 43
c 5
b 3
f 1
dl 0
loc 93
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountryName() 0 9 2
A convertRequestTransferToArray() 0 43 1
A calculateReservationTotal() 0 6 1
A isApplicable() 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\FirstData\Business\Api\Request\Converter;
9
10
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...
11
use Generated\Shared\Transfer\FirstDataApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tDataApiRequestTransfer 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 SprykerEco\Zed\FirstData\FirstDataConfig;
13
14
class ReservationRequestConverter implements FirstDataRequestConverterInterface
15
{
16
    /**
17
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
18
     *
19
     * @return bool
20
     */
21
    public function isApplicable(FirstDataApiRequestTransfer $firstDataApiRequestTransfer): bool
22
    {
23
        return $firstDataApiRequestTransfer->getRequestType() === FirstDataConfig::FIRST_DATA_RESERVATION_REQUEST_TYPE;
24
    }
25
26
    /**
27
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
28
     *
29
     * @return array
30
     */
31
    public function convertRequestTransferToArray(FirstDataApiRequestTransfer $firstDataApiRequestTransfer): array
32
    {
33
        $customerToken = $firstDataApiRequestTransfer->getPaymentMethodOrFail()->getCustomerTokenOrFail();
34
        $billingAddress = $firstDataApiRequestTransfer->getBillingAddressOrFail();
35
        $shippingAddress = $firstDataApiRequestTransfer->getShippingAddressOrFail();
36
37
        return [
38
            'transactionAmount' => [
39
                'total' => $this->calculateReservationTotal($firstDataApiRequestTransfer),
40
                'currency' => $firstDataApiRequestTransfer->getCurrencyIsoCode(),
41
            ],
42
            'paymentMethod' => [
43
                'paymentToken' => [
44
                    'value' => $customerToken->getCardTokenOrFail(),
45
                    'expiryDate' => [
46
                        'month' => $customerToken->getExpMonthOrFail(),
47
                        'year' => $customerToken->getExpYear(),
48
                    ],
49
                ],
50
            ],
51
            'storeId' => $firstDataApiRequestTransfer->getStoreId(),
52
            'order' => [
53
                'orderId' => $firstDataApiRequestTransfer->getOrderOrFail()->getOrderReferenceOrFail(),
54
                'billing' => [
55
                    'name' => $billingAddress->getFirstName() ?? '',
56
                    'customerId' => $billingAddress->getCustomerId() ?? '',
57
                    'address' => [
58
                        'company' => $billingAddress->getCompany() ?? '',
59
                        'address1' => $billingAddress->getAddress1() ?? '',
60
                        'city' => $billingAddress->getCity() ?? '',
61
                        'region' => $billingAddress->getRegion() ?? '',
62
                        'postalCode' => $billingAddress->getZipCode() ?? '',
63
                        'country' => $this->getCountryName($billingAddress),
64
                    ],
65
                ],
66
                'shipping' => [
67
                    'name' => $shippingAddress->getFirstName() ?? '',
68
                    'address' => [
69
                        'address1' => $shippingAddress->getAddress1() ?? '',
70
                        'city' => $shippingAddress->getCity() ?? '',
71
                        'region' => $shippingAddress->getRegion() ?? '',
72
                        'postalCode' => $shippingAddress->getZipCode() ?? '',
73
                        'country' => $this->getCountryName($shippingAddress),
74
                    ],
75
                ],
76
            ],
77
        ];
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
82
     *
83
     * @return string
84
     */
85
    protected function getCountryName(AddressTransfer $addressTransfer): string
86
    {
87
        $countryTransfer = $addressTransfer->getCountry();
88
89
        if ($countryTransfer === null) {
90
            return '';
91
        }
92
93
        return $countryTransfer->getName() ?? '';
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
98
     *
99
     * @return string
100
     */
101
    protected function calculateReservationTotal(FirstDataApiRequestTransfer $firstDataApiRequestTransfer): string
102
    {
103
        $grandTotal = $firstDataApiRequestTransfer->getTotalsOrFail()->getGrandTotal();
104
        $roundedSum = round($grandTotal / 100, 2);
105
106
        return (string)$roundedSum;
107
    }
108
}
109