Passed
Pull Request — master (#46)
by Stanislav
04:42
created

QuoteHydrator::findFirstAvailableShipmentMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.9666
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
        $shipmentMethodTransfer = $this->findFirstAvailableShipmentMethod($quoteTransfer);
133
134
        if ($shipmentMethodTransfer === null) {
135
            $shipmentMethodTransfer = (new ShipmentMethodTransfer())->setCarrierName(static::CARRIER_NAME);
136
        }
137
138
        $shipmentMethodTransfer->setStoreCurrencyPrice(static::DEFAULT_SHIPPING_PRICE);
139
140
        $shipmentTransfer = (new ShipmentTransfer())
141
            ->setMethod($shipmentMethodTransfer)
142
            ->setShipmentSelection($shipmentMethodTransfer->getIdShipmentMethod());
143
144
        $quoteTransfer->setShipment($shipmentTransfer);
145
146
        return $quoteTransfer;
147
    }
148
149
    /**
150
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
151
     *
152
     * @return \ArrayObject|\Generated\Shared\Transfer\ShipmentMethodTransfer|null
153
     */
154
    protected function findFirstAvailableShipmentMethod(QuoteTransfer $quoteTransfer): ?ShipmentMethodTransfer
155
    {
156
        $shipmentMethodsTransfer = $this->shipmentClient->getAvailableMethodsByShipment($quoteTransfer)
157
            ->getShipmentMethods();
158
159
        if ($shipmentMethodsTransfer->count() === 0) {
160
            return null;
161
        }
162
163
        /** @var ShipmentMethodsTransfer $firstShipmentMethodsTransfer */
164
        $firstShipmentMethodsTransfer = $shipmentMethodsTransfer->getIterator()->current();
165
166
        $shipmentMethodTransfer = $firstShipmentMethodsTransfer->getMethods();
167
168
        if ($shipmentMethodTransfer->count() === 0) {
169
            return null;
170
        }
171
172
        return $shipmentMethodTransfer->getIterator()->current();
173
    }
174
175
    /**
176
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
177
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
178
     *
179
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
180
     */
181
    protected function hydrateQuoteWithCustomer(
182
        QuoteTransfer $quoteTransfer,
183
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
184
    ) {
185
        $customerEmail = $details->getEmail();
186
        $customerTransfer = new CustomerTransfer();
187
        $customerTransfer->setEmail($customerEmail);
188
        $customerTransfer = $this->customerClient->getCustomerByEmail($customerTransfer);
189
190
        if (!empty($customerTransfer->getFirstName())) {
191
            $quoteTransfer->setCustomer($customerTransfer);
192
            return $quoteTransfer;
193
        }
194
        $customerTransfer->setIsGuest(true);
195
        $customerTransfer->setFirstName($details->getShippingFirstName());
196
        $customerTransfer->setLastName($details->getShippingLastName());
197
        $customerTransfer->setCompany($details->getShippingCompany());
198
        $customerTransfer->setEmail($customerEmail);
199
        $quoteTransfer->setCustomer($customerTransfer);
200
201
        return $quoteTransfer;
202
    }
203
204
    /**
205
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
206
     * @param \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
207
     *
208
     * @return \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
209
     */
210
    protected function hydrateQuoteWithAddresses(
211
        QuoteTransfer $quoteTransfer,
212
        PayonePaypalExpressCheckoutGenericPaymentResponseTransfer $details
213
    ) {
214
        $address = new AddressTransfer();
215
216
        if ($this->customerClient->isLoggedIn()) {
217
            $address->setEmail($quoteTransfer->getCustomer()->getEmail());
218
        } else {
219
            $address->setEmail($details->getEmail());
220
        }
221
222
        $address->setFirstName($details->getShippingFirstName());
223
        $address->setLastName($details->getShippingLastName());
224
        $address->setCompany($details->getShippingCompany());
225
        $address->setAddress1($details->getShippingStreet());
226
        $address->setAddress2($details->getShippingAddressAdition());
227
        $address->setCity($details->getShippingCity());
228
        $address->setState($details->getShippingState());
229
        $address->setIso2Code($details->getShippingCountry());
230
        $address->setZipCode($details->getShippingZip());
231
        $quoteTransfer->setBillingAddress($address);
232
        $quoteTransfer->setShippingAddress($address);
233
        $quoteTransfer->setBillingSameAsShipping(true);
234
235
        return $quoteTransfer;
236
    }
237
}
238