Passed
Pull Request — feature/eco-3656/dev-paypal-ex... (#40)
by
unknown
10:33 queued 05:24
created

handleBillingAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Computop\Handler\ExpressCheckout;
9
10
use Generated\Shared\Transfer\ComputopPayPalExpressPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...lExpressPaymentTransfer 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\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...
12
use Spryker\Client\Shipment\ShipmentClientInterface;
13
use Spryker\Shared\Kernel\Transfer\TransferInterface;
14
use SprykerEco\Client\Computop\ComputopClientInterface;
15
use SprykerEco\Shared\Computop\ComputopConfig;
16
use SprykerEco\Yves\Computop\ComputopFactory;
17
use SprykerEco\Yves\Computop\Converter\ConverterInterface;
18
use SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface;
19
use SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface;
20
21
class ComputopPayPalExpressInitHandler implements ComputopPayPalExpressInitHandlerInterface
22
{
23
    /**
24
     * @var \SprykerEco\Yves\Computop\Converter\ConverterInterface
25
     */
26
    protected $converter;
27
28
    /**
29
     * @var \SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface
30
     */
31
    protected $quoteClient;
32
33
    /**
34
     * @var \SprykerEco\Client\Computop\ComputopClientInterface
35
     */
36
    protected $computopClient;
37
38
    /**
39
     * @var \Spryker\Client\Shipment\ShipmentClientInterface
40
     */
41
    protected $shipmentClient;
42
43
    /**
44
     * @var \SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface
45
     */
46
    protected $payPalExpressToQuoteMapper;
47
48
    /**
49
     * @param \SprykerEco\Yves\Computop\Converter\ConverterInterface $converter
50
     * @param \SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface $quoteClient
51
     * @param \SprykerEco\Client\Computop\ComputopClientInterface $computopClient
52
     * @param \Spryker\Client\Shipment\ShipmentClientInterface $shipmentClient
53
     * @param \SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface $payPalExpressToQuoteMapper
54
     */
55
    public function __construct(
56
        ConverterInterface $converter,
57
        ComputopToQuoteClientInterface $quoteClient,
58
        ComputopClientInterface $computopClient,
59
        ShipmentClientInterface $shipmentClient,
60
        PayPalExpressToQuoteMapperInterface $payPalExpressToQuoteMapper
61
    ) {
62
        $this->converter = $converter;
63
        $this->quoteClient = $quoteClient;
64
        $this->computopClient = $computopClient;
65
        $this->shipmentClient = $shipmentClient;
66
        $this->payPalExpressToQuoteMapper = $payPalExpressToQuoteMapper;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
71
     * @param array $responseArray
72
     *
73
     * @return \Generated\Shared\Transfer\QuoteTransfer
74
     */
75
    public function handle(QuoteTransfer $quoteTransfer, array $responseArray): QuoteTransfer
76
    {
77
        $responseTransfer = $this->converter->getResponseTransfer($responseArray);
78
        $this->addPaymentToQuote($quoteTransfer, $responseTransfer);
79
        $quoteTransfer = $this->handlePaymentSelection($quoteTransfer);
80
        $quoteTransfer = $this->handleShippingAddress($quoteTransfer);
81
        $quoteTransfer = $this->handleBillingAddress($quoteTransfer);
82
        $quoteTransfer = $this->handleCustomer($quoteTransfer);
83
        $quoteTransfer->setCheckoutConfirmed(true);
84
85
        $this->quoteClient->setQuote($quoteTransfer);
86
87
        return $quoteTransfer;
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
92
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $responseTransfer
93
     *
94
     * @return \Generated\Shared\Transfer\QuoteTransfer
95
     */
96
    protected function addPaymentToQuote(QuoteTransfer $quoteTransfer, TransferInterface $responseTransfer): QuoteTransfer
97
    {
98
        if ($quoteTransfer->getPayment()->getComputopPayPalExpress() === null) {
99
            $computopTransfer = new ComputopPayPalExpressPaymentTransfer();
100
            $quoteTransfer->getPayment()->setComputopPayPalExpress($computopTransfer);
101
        }
102
103
        /** @var \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer */
104
        $quoteTransfer->getPayment()->getComputopPayPalExpress()->setPayPalExpressInitResponse(
105
            $responseTransfer
106
        );
107
108
        return $quoteTransfer;
109
    }
110
111
    /**
112
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
113
     *
114
     * @return \Generated\Shared\Transfer\QuoteTransfer
115
     */
116
    protected function handleShippingAddress(QuoteTransfer $quoteTransfer): QuoteTransfer
117
    {
118
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
119
        if ($payPalInitResponseTransfer->getAddressStreet() === null) {
120
            return $quoteTransfer;
121
        }
122
123
        $quoteTransfer = $this->shipmentClient->expandQuoteWithShipmentGroups($quoteTransfer);
124
125
        return $this->payPalExpressToQuoteMapper->mapAddressTransfer($quoteTransfer, $payPalInitResponseTransfer);
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
130
     *
131
     * @return \Generated\Shared\Transfer\QuoteTransfer
132
     */
133
    protected function handleBillingAddress(QuoteTransfer $quoteTransfer): QuoteTransfer
134
    {
135
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
136
        if ($payPalInitResponseTransfer->getBillingAddressStreet() === null) {
137
            return $quoteTransfer;
138
        }
139
140
        return $this->payPalExpressToQuoteMapper->mapBillingTransfer($quoteTransfer, $payPalInitResponseTransfer);
141
    }
142
143
    /**
144
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
145
     *
146
     * @return \Generated\Shared\Transfer\QuoteTransfer
147
     */
148
    protected function handleCustomer(QuoteTransfer $quoteTransfer): QuoteTransfer
149
    {
150
        if (!$quoteTransfer->getCustomer()->getIsGuest()) {
151
            return $quoteTransfer;
152
        }
153
154
        $payPalInitResponseTransfer = $quoteTransfer
155
            ->getPayment()
156
            ->getComputopPayPalExpress()
157
            ->getPayPalExpressInitResponse();
158
159
        return $this->payPalExpressToQuoteMapper->mapCustomerTransfer($quoteTransfer, $payPalInitResponseTransfer);
160
    }
161
162
    /**
163
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
164
     *
165
     * @return \Generated\Shared\Transfer\QuoteTransfer
166
     */
167
    protected function handlePaymentSelection(QuoteTransfer $quoteTransfer): QuoteTransfer
168
    {
169
        $quoteTransfer->getPayment()->setPaymentSelection(ComputopConfig::PAYMENT_METHOD_PAY_PAL_EXPRESS);
170
        $handler = (new ComputopFactory())->createComputopPaymentHandler();
171
172
        return $handler->addPaymentToQuote($quoteTransfer);
173
    }
174
}
175