Passed
Pull Request — master (#46)
by Stanislav
06:08
created

QuoteHydrator::getShipmentMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
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\ShipmentMethodsTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ShipmentMethodsTransfer 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\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...
20
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...
21
use Spryker\Shared\Kernel\Store;
22
use SprykerEco\Shared\Payone\PayoneApiConstants;
23
use SprykerEco\Shared\Payone\PayoneConstants;
24
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface;
25
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface;
26
use SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface;
27
28
class QuoteHydrator implements QuoteHydratorInterface
29
{
30
    /**
31
     * @const string CARRIER_NAME
32
     */
33
    public const CARRIER_NAME = 'Paypal';
34
35
    /**
36
     * @const int DEFAULT_SHIPPING_PRICE
37
     */
38
    public const DEFAULT_SHIPPING_PRICE = 0;
39
40
    /**
41
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface
42
     */
43
    protected $customerClient;
44
45
    /**
46
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface
47
     */
48
    protected $shipmentClient;
49
50
    /**
51
     * @var \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface
52
     */
53
    protected $calculationClient;
54
55
    /**
56
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToShipmentInterface $shipmentClient
57
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCustomerInterface $customerClient
58
     * @param \SprykerEco\Yves\Payone\Dependency\Client\PayoneToCalculationInterface $calculationClient
59
     */
60
    public function __construct(
61
        PayoneToShipmentInterface $shipmentClient,
62
        PayoneToCustomerInterface $customerClient,
63
        PayoneToCalculationInterface $calculationClient
64
    ) {
65
        $this->shipmentClient = $shipmentClient;
66
        $this->customerClient = $customerClient;
67
        $this->calculationClient = $calculationClient;
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
72
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
73
     *
74
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
75
     */
76
    public function getHydratedQuote(
77
        QuoteTransfer $quoteTransfer,
78
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
79
    ) {
80
        $quoteTransfer = $this->hydrateQuoteWithPayment($quoteTransfer);
81
        $quoteTransfer = $this->hydrateQuoteWithShipment($quoteTransfer);
82
        $quoteTransfer = $this->hydrateQuoteWithAddresses($quoteTransfer, $details);
83
        if (!$this->customerClient->isLoggedIn()) {
84
            $quoteTransfer = $this->hydrateQuoteWithCustomer($quoteTransfer, $details);
85
            $quoteTransfer->setIsGuestExpressCheckout(true);
86
        }
87
        return $this->calculationClient->recalculate($quoteTransfer);
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
92
     *
93
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
94
     */
95
    protected function hydrateQuoteWithPayment(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(Store::getInstance()->getCurrencyIsoCode())
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 $quoteTransfer
125
     */
126
    protected function hydrateQuoteWithShipment($quoteTransfer)
127
    {
128
        if ($quoteTransfer->getShipment()) {
129
            return $quoteTransfer;
130
        }
131
132
        $shipmentTransfer = new ShipmentTransfer();
133
134
        $shipmentTransfer->setMethod(
135
            (new ShipmentMethodTransfer())
136
                ->setCarrierName(static::CARRIER_NAME)
137
                ->setStoreCurrencyPrice(static::DEFAULT_SHIPPING_PRICE)
138
        );
139
140
        $quoteTransfer->setShipment($shipmentTransfer);
141
142
        $shippingMethod = $this->getShipmentMethod($quoteTransfer);
143
144
        if ($shippingMethod === null) {
145
            return $quoteTransfer;
146
        }
147
148
        $shippingMethod->setStoreCurrencyPrice(static::DEFAULT_SHIPPING_PRICE);
149
        $shipmentTransfer->setMethod($shippingMethod);
150
        $shipmentTransfer->setShipmentSelection($shippingMethod->getIdShipmentMethod());
151
        $quoteTransfer->setShipment($shipmentTransfer);
152
153
        return $quoteTransfer;
154
    }
155
156
    /**
157
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
158
     *
159
     * @return \ArrayObject|\Generated\Shared\Transfer\ShipmentMethodTransfer|null
160
     */
161
    protected function getShipmentMethod(QuoteTransfer $quoteTransfer): ?ShipmentMethodTransfer
162
    {
163
        $shipmentMethodsCollection = $this->shipmentClient->getAvailableMethodsByShipment($quoteTransfer)
164
            ->getShipmentMethods();
165
166
        if ($shipmentMethodsCollection->count() === 0) {
167
            return null;
168
        }
169
170
        /** @var ShipmentMethodsTransfer $shipmentMethods */
171
        $shipmentMethods = $shipmentMethodsCollection->getIterator()->current();
172
173
        $methods = $shipmentMethods->getMethods();
174
175
        if ($methods->count() === 0) {
176
            return null;
177
        }
178
179
        return $methods->getIterator()->current();
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
184
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
185
     *
186
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
187
     */
188
    protected function hydrateQuoteWithCustomer(
189
        QuoteTransfer $quoteTransfer,
190
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
191
    ) {
192
        $customerEmail = $details->getEmail();
193
        $customerTransfer = new CustomerTransfer();
194
        $customerTransfer->setEmail($customerEmail);
195
        $customerTransfer = $this->customerClient->getCustomerByEmail($customerTransfer);
196
197
        if (!empty($customerTransfer->getFirstName())) {
198
            $quoteTransfer->setCustomer($customerTransfer);
199
            return $quoteTransfer;
200
        }
201
        $customerTransfer->setIsGuest(true);
202
        $customerTransfer->setFirstName($details->getShippingFirstName());
203
        $customerTransfer->setLastName($details->getShippingLastName());
204
        $customerTransfer->setCompany($details->getShippingCompany());
205
        $customerTransfer->setEmail($customerEmail);
206
        $quoteTransfer->setCustomer($customerTransfer);
207
208
        return $quoteTransfer;
209
    }
210
211
    /**
212
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
213
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
214
     *
215
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
216
     */
217
    protected function hydrateQuoteWithAddresses(
218
        QuoteTransfer $quoteTransfer,
219
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
220
    ) {
221
        $address = new AddressTransfer();
222
223
        if ($this->customerClient->isLoggedIn()) {
224
            $address->setEmail($quoteTransfer->getCustomer()->getEmail());
225
        } else {
226
            $address->setEmail($details->getEmail());
227
        }
228
229
        $address->setFirstName($details->getShippingFirstName());
230
        $address->setLastName($details->getShippingLastName());
231
        $address->setCompany($details->getShippingCompany());
232
        $address->setAddress1($details->getShippingStreet());
233
        $address->setAddress2($details->getShippingAddressAdition());
234
        $address->setCity($details->getShippingCity());
235
        $address->setState($details->getShippingState());
236
        $address->setIso2Code($details->getShippingCountry());
237
        $address->setZipCode($details->getShippingZip());
238
        $quoteTransfer->setBillingAddress($address);
239
        $quoteTransfer->setShippingAddress($address);
240
        $quoteTransfer->setBillingSameAsShipping(true);
241
242
        return $quoteTransfer;
243
    }
244
}
245