Passed
Push — dev ( 1c92b1...65ef69 )
by Aleksey
09:25
created

OrderToHeidelpayRequest::formatDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Heidelpay\Business\Mapper;
9
10
use DateTime;
11
use Generated\Shared\Transfer\AddressTransfer;
1 ignored issue
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\CustomerTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer 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\HeidelpayCustomerAddressTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...CustomerAddressTransfer 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\HeidelpayCustomerPurchaseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ustomerPurchaseTransfer 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\HeidelpayRequestTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\HeidelpayRequestTransfer 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\HeidelpayRiskInformationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...RiskInformationTransfer 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\OrderTransfer;
1 ignored issue
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...
18
use SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface;
19
20
class OrderToHeidelpayRequest implements OrderToHeidelpayRequestInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface
24
     */
25
    protected $moneyFacade;
26
27
    /**
28
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Facade\HeidelpayToMoneyFacadeInterface $moneyFacade
29
     */
30
    public function __construct(HeidelpayToMoneyFacadeInterface $moneyFacade)
31
    {
32
        $this->moneyFacade = $moneyFacade;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
37
     * @param \Generated\Shared\Transfer\HeidelpayRequestTransfer $heidelpayRequestTransfer
38
     *
39
     * @return \Generated\Shared\Transfer\HeidelpayRequestTransfer
40
     */
41
    public function map(OrderTransfer $orderTransfer, HeidelpayRequestTransfer $heidelpayRequestTransfer): HeidelpayRequestTransfer
42
    {
43
        $heidelpayRequestTransfer = $this->mapCustomerAddress($orderTransfer, $heidelpayRequestTransfer);
44
        $heidelpayRequestTransfer = $this->mapOrderInformation($orderTransfer, $heidelpayRequestTransfer);
45
        $heidelpayRequestTransfer = $this->mapOrderPayment($orderTransfer, $heidelpayRequestTransfer);
46
        $heidelpayRequestTransfer = $this->mapCustomerInformation($orderTransfer, $heidelpayRequestTransfer);
47
48
        return $heidelpayRequestTransfer;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
53
     * @param \Generated\Shared\Transfer\HeidelpayRequestTransfer $heidelpayRequestTransfer
54
     *
55
     * @return \Generated\Shared\Transfer\HeidelpayRequestTransfer
56
     */
57
    protected function mapCustomerAddress(OrderTransfer $orderTransfer, HeidelpayRequestTransfer $heidelpayRequestTransfer): HeidelpayRequestTransfer
58
    {
59
        $heidelpayRequestTransfer->setCustomerAddress(
60
            (new HeidelpayCustomerAddressTransfer())
61
                ->setCity($orderTransfer->getBillingAddress()->getCity())
62
                ->setCompany($orderTransfer->getBillingAddress()->getCompany())
63
                ->setCountry($orderTransfer->getBillingAddress()->getIso2Code())
64
                ->setEmail($orderTransfer->getEmail())
65
                ->setFirstName($orderTransfer->getBillingAddress()->getFirstName())
66
                ->setLastName($orderTransfer->getBillingAddress()->getLastName())
67
                ->setState($orderTransfer->getBillingAddress()->getState())
68
                ->setStreet($this->getFullStreetName($orderTransfer->getBillingAddress()))
69
                ->setZip($orderTransfer->getBillingAddress()->getZipCode())
70
        );
71
72
        return $heidelpayRequestTransfer;
73
    }
74
75
    /**
76
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
77
     * @param \Generated\Shared\Transfer\HeidelpayRequestTransfer $heidelpayRequestTransfer
78
     *
79
     * @return \Generated\Shared\Transfer\HeidelpayRequestTransfer
80
     */
81
    protected function mapOrderInformation(OrderTransfer $orderTransfer, HeidelpayRequestTransfer $heidelpayRequestTransfer): HeidelpayRequestTransfer
82
    {
83
        $heidelpayRequestTransfer->setCustomerPurchase(
84
            (new HeidelpayCustomerPurchaseTransfer())
85
                ->setAmount($this->getOrderGrandTotalInDecimal($orderTransfer))
86
                ->setIdOrder((string)$orderTransfer->getIdSalesOrder())
87
        );
88
89
        return $heidelpayRequestTransfer;
90
    }
91
92
    /**
93
     * @param \Generated\Shared\Transfer\AddressTransfer $shippingAddressTransfer
94
     *
95
     * @return string
96
     */
97
    protected function getFullStreetName(AddressTransfer $shippingAddressTransfer): string
98
    {
99
        return sprintf('%s %s', $shippingAddressTransfer->getAddress1(), $shippingAddressTransfer->getAddress2());
100
    }
101
102
    /**
103
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
104
     *
105
     * @return float
106
     */
107
    protected function getOrderGrandTotalInDecimal(OrderTransfer $orderTransfer): float
108
    {
109
        $orderAmountInt = $orderTransfer->getTotals()->getGrandTotal();
110
        $orderAmountDecimal = $this->moneyFacade->convertIntegerToDecimal($orderAmountInt);
111
112
        return $orderAmountDecimal;
113
    }
114
115
    /**
116
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
117
     * @param \Generated\Shared\Transfer\HeidelpayRequestTransfer $heidelpayRequestTransfer
118
     *
119
     * @return \Generated\Shared\Transfer\HeidelpayRequestTransfer
120
     */
121
    protected function mapOrderPayment(OrderTransfer $orderTransfer, HeidelpayRequestTransfer $heidelpayRequestTransfer): HeidelpayRequestTransfer
122
    {
123
        $heidelpayPayment = $orderTransfer->getHeidelpayPayment();
124
        $heidelpayRequestTransfer
125
            ->setIdBasket($heidelpayPayment->getIdBasket())
126
            ->setIdPaymentRegistration($heidelpayPayment->getIdPaymentRegistration())
127
            ->setIdPaymentReference($heidelpayPayment->getIdPaymentReference());
128
129
        return $heidelpayRequestTransfer;
130
    }
131
132
    /**
133
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
134
     * @param \Generated\Shared\Transfer\HeidelpayRequestTransfer $heidelpayRequestTransfer
135
     *
136
     * @return \Generated\Shared\Transfer\HeidelpayRequestTransfer
137
     */
138
    protected function mapCustomerInformation(OrderTransfer $orderTransfer, HeidelpayRequestTransfer $heidelpayRequestTransfer): HeidelpayRequestTransfer
139
    {
140
        $customerRegistrationDate = $this->findCustomerRegistrationDate($orderTransfer->getCustomer());
141
142
        $heidelpayRequestTransfer->setRiskInformation(
143
            (new HeidelpayRiskInformationTransfer())
144
                ->setIsCustomerGuest((bool)$orderTransfer->getCustomer()->getIsGuest())
145
                ->setCustomerSince($customerRegistrationDate)
146
                ->setCustomerId($orderTransfer->getCustomer()->getIdCustomer())
147
        );
148
149
        return $heidelpayRequestTransfer;
150
    }
151
152
    /**
153
     * @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
154
     *
155
     * @return string|null
156
     */
157
    protected function findCustomerRegistrationDate(CustomerTransfer $customerTransfer): ?string
158
    {
159
        $createdAtDate = $customerTransfer->getCreatedAt();
160
        $createdAtDateFormatted = $createdAtDate ? $this->formatDate($createdAtDate) : $createdAtDate;
161
162
        return $createdAtDateFormatted;
163
    }
164
165
    /**
166
     * @param string $date
167
     *
168
     * @return string
169
     */
170
    protected function formatDate(string $date): string
171
    {
172
        return (new DateTime($date))->format('Y-m-d');
173
    }
174
}
175