Passed
Pull Request — feature/eco-3656/dev-paypal-ex... (#40)
by
unknown
17:08 queued 08:59
created

ComputopPayPalExpressInitHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
eloc 50
dl 0
loc 167
rs 10
c 2
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addBillingAddressToQuote() 0 13 2
A __construct() 0 14 1
A addShippingAddressToQuote() 0 12 2
A addPaymentToQuote() 0 13 2
A addPaymentSelectionToQuote() 0 5 1
A addCustomerToQuote() 0 11 3
A handle() 0 15 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\Shared\Kernel\Transfer\TransferInterface;
13
use SprykerEco\Client\Computop\ComputopClientInterface;
14
use SprykerEco\Shared\Computop\ComputopConfig;
15
use SprykerEco\Yves\Computop\Converter\ConverterInterface;
16
use SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface;
17
use SprykerEco\Yves\Computop\Dependency\Client\ComputopToShipmentClientInterface;
18
use SprykerEco\Yves\Computop\Handler\ComputopPaymentHandlerInterface;
19
use SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface;
20
21
class ComputopPayPalExpressInitHandler implements ComputopPayPalExpressInitHandlerInterface
22
{
23
    /**
24
     * @var \SprykerEco\Yves\Computop\Handler\ComputopPaymentHandlerInterface
25
     */
26
    protected $computopPaymentHandler;
27
28
    /**
29
     * @var \SprykerEco\Yves\Computop\Converter\ConverterInterface
30
     */
31
    protected $converter;
32
33
    /**
34
     * @var \SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface
35
     */
36
    protected $quoteClient;
37
38
    /**
39
     * @var \SprykerEco\Client\Computop\ComputopClientInterface
40
     */
41
    protected $computopClient;
42
43
    /**
44
     * @var \SprykerEco\Yves\Computop\Dependency\Client\ComputopToShipmentClientInterface
45
     */
46
    protected $shipmentClient;
47
48
    /**
49
     * @var \SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface
50
     */
51
    protected $payPalExpressToQuoteMapper;
52
53
    /**
54
     * @param \SprykerEco\Yves\Computop\Handler\ComputopPaymentHandlerInterface $computopPaymentHandler
55
     * @param \SprykerEco\Yves\Computop\Converter\ConverterInterface $converter
56
     * @param \SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface $quoteClient
57
     * @param \SprykerEco\Client\Computop\ComputopClientInterface $computopClient
58
     * @param \SprykerEco\Yves\Computop\Dependency\Client\ComputopToShipmentClientInterface $shipmentClient
59
     * @param \SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface $payPalExpressToQuoteMapper
60
     */
61
    public function __construct(
62
        ComputopPaymentHandlerInterface $computopPaymentHandler,
63
        ConverterInterface $converter,
64
        ComputopToQuoteClientInterface $quoteClient,
65
        ComputopClientInterface $computopClient,
66
        ComputopToShipmentClientInterface $shipmentClient,
67
        PayPalExpressToQuoteMapperInterface $payPalExpressToQuoteMapper
68
    ) {
69
        $this->computopPaymentHandler = $computopPaymentHandler;
70
        $this->converter = $converter;
71
        $this->quoteClient = $quoteClient;
72
        $this->computopClient = $computopClient;
73
        $this->shipmentClient = $shipmentClient;
74
        $this->payPalExpressToQuoteMapper = $payPalExpressToQuoteMapper;
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
79
     * @param array $responseArray
80
     *
81
     * @return \Generated\Shared\Transfer\QuoteTransfer
82
     */
83
    public function handle(QuoteTransfer $quoteTransfer, array $responseArray): QuoteTransfer
84
    {
85
        $responseTransfer = $this->converter->getResponseTransfer($responseArray);
86
87
        $quoteTransfer = $this->addPaymentToQuote($quoteTransfer, $responseTransfer);
88
        $quoteTransfer = $this->addPaymentSelectionToQuote($quoteTransfer);
89
        $quoteTransfer = $this->addShippingAddressToQuote($quoteTransfer);
90
        $quoteTransfer = $this->addBillingAddressToQuote($quoteTransfer);
91
        $quoteTransfer = $this->addCustomerToQuote($quoteTransfer);
92
93
        $quoteTransfer->setCheckoutConfirmed(true);
94
95
        $this->quoteClient->setQuote($quoteTransfer);
96
97
        return $quoteTransfer;
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
102
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $responseTransfer
103
     *
104
     * @return \Generated\Shared\Transfer\QuoteTransfer
105
     */
106
    protected function addPaymentToQuote(QuoteTransfer $quoteTransfer, TransferInterface $responseTransfer): QuoteTransfer
107
    {
108
        if ($quoteTransfer->getPaymentOrFail()->getComputopPayPalExpress() === null) {
109
            $computopTransfer = new ComputopPayPalExpressPaymentTransfer();
110
            $quoteTransfer->getPaymentOrFail()->setComputopPayPalExpress($computopTransfer);
111
        }
112
113
        /** @var \Generated\Shared\Transfer\ComputopPayPalExpressInitResponseTransfer $responseTransfer */
114
        $quoteTransfer->getPaymentOrFail()->getComputopPayPalExpress()->setPayPalExpressInitResponse(
115
            $responseTransfer
116
        );
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 addShippingAddressToQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
127
    {
128
        $payPalInitResponseTransfer = $quoteTransfer->getPaymentOrFail()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
129
        if ($payPalInitResponseTransfer->getAddressStreet() === null) {
130
            return $quoteTransfer;
131
        }
132
133
        $quoteTransfer = $this->shipmentClient->expandQuoteWithShipmentGroups($quoteTransfer);
134
135
        return $this
136
            ->payPalExpressToQuoteMapper
137
            ->mapAddressFromComputopPayPalExpressInitResponseToQuote($payPalInitResponseTransfer, $quoteTransfer);
138
    }
139
140
    /**
141
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
142
     *
143
     * @return \Generated\Shared\Transfer\QuoteTransfer
144
     */
145
    protected function addBillingAddressToQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
146
    {
147
        $payPalInitResponseTransfer = $quoteTransfer
148
            ->getPaymentOrFail()
149
            ->getComputopPayPalExpressOrFail()->getPayPalExpressInitResponseOrFail();
150
151
        if ($payPalInitResponseTransfer->getBillingAddressStreet() === null) {
152
            $quoteTransfer->setBillingAddress($quoteTransfer->getShippingAddress());
153
154
            return $quoteTransfer->setBillingSameAsShipping(true);
155
        }
156
157
        return $this->payPalExpressToQuoteMapper->mapBillingAddressFromComputopPayPalExpressInitResponseToQuote($payPalInitResponseTransfer, $quoteTransfer);
158
    }
159
160
    /**
161
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
162
     *
163
     * @return \Generated\Shared\Transfer\QuoteTransfer
164
     */
165
    protected function addCustomerToQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
166
    {
167
        if ($quoteTransfer->getCustomer()->getIsGuest() !== null && !$quoteTransfer->getCustomer()->getIsGuest()) {
168
            return $quoteTransfer;
169
        }
170
171
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()
172
            ->getComputopPayPalExpress()
173
            ->getPayPalExpressInitResponse();
174
175
        return $this->payPalExpressToQuoteMapper->mapCustomerFromComputopPayPalExpressInitResponseToQuote($payPalInitResponseTransfer, $quoteTransfer);
176
    }
177
178
    /**
179
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
180
     *
181
     * @return \Generated\Shared\Transfer\QuoteTransfer
182
     */
183
    protected function addPaymentSelectionToQuote(QuoteTransfer $quoteTransfer): QuoteTransfer
184
    {
185
        $quoteTransfer->getPaymentOrFail()->setPaymentSelection(ComputopConfig::PAYMENT_METHOD_PAY_PAL_EXPRESS);
186
187
        return $this->computopPaymentHandler->addPaymentToQuote($quoteTransfer);
188
    }
189
}
190