QuoteHydrator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 10
Bugs 1 Features 0
Metric Value
eloc 84
c 10
b 1
f 0
dl 0
loc 191
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A hydrateQuoteWithPayment() 0 24 1
A getHydratedQuote() 0 13 2
A hydrateQuoteWithCustomer() 0 22 2
A hydrateQuoteWithAddresses() 0 26 2
A hydrateQuoteWithShipment() 0 28 3
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\Yves\Payone\Handler\ExpressCheckout;
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\CustomerTransfer;
0 ignored issues
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...
12
use Generated\Shared\Transfer\PaymentDetailTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentDetailTransfer 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\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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\PayonePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayonePaymentTransfer 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\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentResponseTransfer 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\PayonePaypalExpressCheckoutTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ExpressCheckoutTransfer 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\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...
18
use Generated\Shared\Transfer\ShipmentMethodTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodTransfer 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\ShipmentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentTransfer 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 SprykerEco\Shared\Payone\PayoneApiConstants;
21
use SprykerEco\Shared\Payone\PayoneConstants;
22
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface;
23
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface;
24
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface;
25
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToStoreClientInterface;
26
27
class QuoteHydrator implements QuoteHydratorInterface
28
{
29
    /**
30
     * @const string CARRIER_NAME
31
     */
32
    public const CARRIER_NAME = 'Paypal';
33
34
    /**
35
     * @const int DEFAULT_SHIPPING_PRICE
36
     */
37
    public const DEFAULT_SHIPPING_PRICE = 0;
38
39
    /**
40
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface
41
     */
42
    protected $customerClient;
43
44
    /**
45
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface
46
     */
47
    protected $shipmentClient;
48
49
    /**
50
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface
51
     */
52
    protected $calculationClient;
53
54
    /**
55
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface $shipmentClient
56
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface $customerClient
57
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface $calculationClient
58
     */
59
    public function __construct(
60
        PayoneToShipmentInterface $shipmentClient,
61
        PayoneToCustomerInterface $customerClient,
62
        PayoneToCalculationInterface $calculationClient
63
    ) {
64
        $this->shipmentClient = $shipmentClient;
65
        $this->customerClient = $customerClient;
66
        $this->calculationClient = $calculationClient;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
71
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
72
     *
73
     * @return \Generated\Shared\Transfer\QuoteTransfer
74
     */
75
    public function getHydratedQuote(
76
        QuoteTransfer $quoteTransfer,
77
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
78
    ): QuoteTransfer {
79
        $quoteTransfer = $this->hydrateQuoteWithPayment($quoteTransfer);
80
        $quoteTransfer = $this->hydrateQuoteWithShipment($quoteTransfer);
81
        $quoteTransfer = $this->hydrateQuoteWithAddresses($quoteTransfer, $details);
82
        if (!$this->customerClient->isLoggedIn()) {
83
            $quoteTransfer = $this->hydrateQuoteWithCustomer($quoteTransfer, $details);
84
            $quoteTransfer->setIsGuestExpressCheckout(true);
85
        }
86
87
        return $this->calculationClient->recalculate($quoteTransfer);
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
92
     *
93
     * @return \Generated\Shared\Transfer\QuoteTransfer
94
     */
95
    protected function hydrateQuoteWithPayment(QuoteTransfer $quoteTransfer): QuoteTransfer
96
    {
97
        $paymentTransfer = new PaymentTransfer();
98
        $payone = new PayonePaymentTransfer();
99
        $paymentDetailTransfer = new PaymentDetailTransfer();
100
        $paymentDetailTransfer
101
            ->setAmount($quoteTransfer->getTotals()->getGrandTotal())
102
            ->setType(PayoneApiConstants::E_WALLET_TYPE_PAYPAL)
103
            ->setCurrency($quoteTransfer->getCurrencyOrFail()->getCodeOrFail())
104
            ->setWorkOrderId(
105
                $quoteTransfer->getPayment()->getPayonePaypalExpressCheckout()->getWorkOrderId()
106
            );
107
        $payone->setPaymentMethod(PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT);
108
        $payone->setPaymentDetail($paymentDetailTransfer);
109
110
        $paymentTransfer->setPayone($payone);
111
        $paymentTransfer->setPaymentSelection(PayoneConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT_STATE_MACHINE);
112
        $paymentTransfer->setPaymentMethod(PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT);
113
        $paymentTransfer->setPaymentProvider(PayoneConstants::PROVIDER_NAME);
114
        $paypalExpressCheckoutPayment = new PayonePaypalExpressCheckoutTransfer();
115
        $paymentTransfer->setPayonePaypalExpressCheckout($paypalExpressCheckoutPayment);
116
        $quoteTransfer->setPayment($paymentTransfer);
117
118
        return $quoteTransfer;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
123
     *
124
     * @return \Generated\Shared\Transfer\QuoteTransfer
125
     */
126
    protected function hydrateQuoteWithShipment(QuoteTransfer $quoteTransfer): QuoteTransfer
127
    {
128
        if ($quoteTransfer->getShipment()) {
129
            return $quoteTransfer;
130
        }
131
132
        $shipmentTransfer = new ShipmentTransfer();
133
134
        $methods = $this->shipmentClient->getAvailableMethods($quoteTransfer)->getMethods();
135
        $shippingMethod = reset($methods);
136
137
        if ($shippingMethod) {
138
            $shippingMethod->setStoreCurrencyPrice(static::DEFAULT_SHIPPING_PRICE);
139
            $shipmentTransfer->setMethod($shippingMethod);
140
            $shipmentTransfer->setShipmentSelection($shippingMethod->getIdShipmentMethod());
141
            $quoteTransfer->setShipment($shipmentTransfer);
142
143
            return $quoteTransfer;
144
        }
145
146
        $shipmentTransfer->setMethod(
147
            (new ShipmentMethodTransfer())
148
                ->setCarrierName(static::CARRIER_NAME)
149
                ->setStoreCurrencyPrice(static::DEFAULT_SHIPPING_PRICE)
150
        );
151
        $quoteTransfer->setShipment($shipmentTransfer);
152
153
        return $quoteTransfer;
154
    }
155
156
    /**
157
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
158
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
159
     *
160
     * @return \Generated\Shared\Transfer\QuoteTransfer
161
     */
162
    protected function hydrateQuoteWithCustomer(
163
        QuoteTransfer $quoteTransfer,
164
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
165
    ): QuoteTransfer {
166
        $customerEmail = $details->getEmail();
167
        $customerTransfer = new CustomerTransfer();
168
        $customerTransfer->setEmail($customerEmail);
169
        $customerTransfer = $this->customerClient->getCustomerByEmail($customerTransfer);
170
171
        if (!empty($customerTransfer->getFirstName())) {
172
            $quoteTransfer->setCustomer($customerTransfer);
173
174
            return $quoteTransfer;
175
        }
176
        $customerTransfer->setIsGuest(true);
177
        $customerTransfer->setFirstName($details->getShippingFirstName());
178
        $customerTransfer->setLastName($details->getShippingLastName());
179
        $customerTransfer->setCompany($details->getShippingCompany());
180
        $customerTransfer->setEmail($customerEmail);
181
        $quoteTransfer->setCustomer($customerTransfer);
182
183
        return $quoteTransfer;
184
    }
185
186
    /**
187
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
188
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
189
     *
190
     * @return \Generated\Shared\Transfer\QuoteTransfer
191
     */
192
    protected function hydrateQuoteWithAddresses(
193
        QuoteTransfer $quoteTransfer,
194
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
195
    ): QuoteTransfer {
196
        $address = new AddressTransfer();
197
198
        if ($this->customerClient->isLoggedIn()) {
199
            $address->setEmail($quoteTransfer->getCustomer()->getEmail());
200
        } else {
201
            $address->setEmail($details->getEmail());
202
        }
203
204
        $address->setFirstName($details->getShippingFirstName());
205
        $address->setLastName($details->getShippingLastName());
206
        $address->setCompany($details->getShippingCompany());
207
        $address->setAddress1($details->getShippingStreet());
208
        $address->setAddress2($details->getShippingAddressAdition());
209
        $address->setCity($details->getShippingCity());
210
        $address->setState($details->getShippingState());
211
        $address->setIso2Code($details->getShippingCountry());
212
        $address->setZipCode($details->getShippingZip());
213
        $quoteTransfer->setBillingAddress($address);
214
        $quoteTransfer->setShippingAddress($address);
215
        $quoteTransfer->setBillingSameAsShipping(true);
216
217
        return $quoteTransfer;
218
    }
219
}
220