Completed
Push — feature/eco-3656/eco-3658-enab... ( 4f4220...78bea4 )
by
unknown
04:14
created

ComputopPayPalExpressInitHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 38
c 0
b 0
f 0
dl 0
loc 130
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A addPaymentToQuote() 0 12 2
A handleCustomer() 0 9 2
A handlePaymentSelection() 0 7 1
A handleBillingAddress() 0 9 2
A handleShippingAddress() 0 10 2
A handle() 0 24 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\Quote\QuoteClientInterface;
13
use Spryker\Shared\Kernel\Transfer\TransferInterface;
14
use SprykerEco\Shared\Computop\ComputopConfig;
15
use SprykerEco\Yves\Computop\ComputopFactory;
16
use SprykerEco\Yves\Computop\Converter\ConverterInterface;
17
use SprykerEco\Yves\Computop\Dependency\Client\ComputopToQuoteClientInterface;
18
use SprykerEco\Yves\Computop\Handler\ComputopPrePostPaymentHandlerInterface;
19
use SprykerEco\Yves\Computop\Mapper\Init\PrePlace\PayPalExpressToQuoteMapperInterface;
20
21
class ComputopPayPalExpressInitHandler implements ComputopPayPalExpressInitHandlerInterface
22
{
23
    /**
24
     * @var QuoteClientInterface
25
     */
26
    protected $quoteClient;
27
28
    /**
29
     * @var PayPalExpressToQuoteMapperInterface
30
     */
31
    private $payPalExpressToQuoteMapper;
32
33
    /**
34
     * @var ConverterInterface
35
     */
36
    private $converter;
37
38
    public function __construct(
39
        ConverterInterface $converter,
40
        ComputopToQuoteClientInterface $quoteClient,
41
        PayPalExpressToQuoteMapperInterface $payPalExpressToQuoteMapper
42
    )
43
    {
44
        $this->quoteClient = $quoteClient;
0 ignored issues
show
Documentation Bug introduced by
It seems like $quoteClient of type SprykerEco\Yves\Computop...pToQuoteClientInterface is incompatible with the declared type Spryker\Client\Quote\QuoteClientInterface of property $quoteClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->payPalExpressToQuoteMapper = $payPalExpressToQuoteMapper;
46
        $this->converter = $converter;
47
    }
48
49
    public function handle(QuoteTransfer $quoteTransfer, array $responseArray)
50
    {
51
        $responseTransfer = $this->converter->getResponseTransfer($responseArray);
52
        $this->addPaymentToQuote($quoteTransfer, $responseTransfer);
53
        //fill the quote
54
        //fill address
55
        $quoteTransfer = $this->handleShippingAddress($quoteTransfer);
56
        $quoteTransfer = $this->handleBillingAddress($quoteTransfer);
57
        //fill customer
58
        $quoteTransfer = $this->handleCustomer($quoteTransfer);
59
        //fill payment
60
        $quoteTransfer = $this->handlePaymentSelection($quoteTransfer);
61
62
        $quoteTransfer->setCheckoutConfirmed(true);
63
64
65
        //place order
66
67
68
        //confirm order
69
70
        $this->quoteClient->setQuote($quoteTransfer);
71
72
        return $quoteTransfer;
73
    }
74
75
    /**
76
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
77
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $responseTransfer
78
     *
79
     * @return \Generated\Shared\Transfer\QuoteTransfer
80
     */
81
    protected function addPaymentToQuote(QuoteTransfer $quoteTransfer, TransferInterface $responseTransfer): QuoteTransfer
82
    {
83
        if ($quoteTransfer->getPayment()->getComputopPayPalExpress() === null) {
84
            $computopTransfer = new ComputopPayPalExpressPaymentTransfer();
85
            $quoteTransfer->getPayment()->setComputopPayPalExpress($computopTransfer);
86
        }
87
88
        $quoteTransfer->getPayment()->getComputopPayPalExpress()->setPayPalExpressInitResponse(
89
            $responseTransfer
90
        );
91
92
        return $quoteTransfer;
93
    }
94
95
    /**
96
     * @param QuoteTransfer $quoteTransfer
97
     *
98
     * @return QuoteTransfer
99
     */
100
    protected function handleShippingAddress(QuoteTransfer $quoteTransfer): QuoteTransfer
101
    {
102
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
103
        if ($payPalInitResponseTransfer->getAddressStreet() === null) {
104
            return $quoteTransfer;
105
        }
106
107
        $quoteTransfer = (new \SprykerEco\Client\Computop\ComputopFactory())->getShipmentClient()->expandQuoteWithShipmentGroups($quoteTransfer);
108
109
        return $this->payPalExpressToQuoteMapper->mapAddressTransfer($quoteTransfer, $payPalInitResponseTransfer);
110
    }
111
112
    /**
113
     * @param QuoteTransfer $quoteTransfer
114
     *
115
     * @return QuoteTransfer
116
     */
117
    protected function handleBillingAddress(QuoteTransfer $quoteTransfer): QuoteTransfer
118
    {
119
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
120
121
        if ($payPalInitResponseTransfer->getBillingAddressStreet() === null) {
122
            return $quoteTransfer;
123
        }
124
125
        return $this->payPalExpressToQuoteMapper->mapBillingTransfer($quoteTransfer, $payPalInitResponseTransfer);
126
    }
127
128
    /**
129
     * @param QuoteTransfer $quoteTransfer
130
     *
131
     * @return QuoteTransfer
132
     */
133
    protected function handleCustomer(QuoteTransfer $quoteTransfer): ?QuoteTransfer
134
    {
135
        if (!$quoteTransfer->getCustomer()->getIsGuest()) {
136
            return $quoteTransfer;
137
        }
138
139
        $payPalInitResponseTransfer = $quoteTransfer->getPayment()->getComputopPayPalExpress()->getPayPalExpressInitResponse();
140
141
        return $this->payPalExpressToQuoteMapper->mapCustomerTransfer($quoteTransfer, $payPalInitResponseTransfer);
142
    }
143
144
    protected function handlePaymentSelection(QuoteTransfer $quoteTransfer)
145
    {
146
        $quoteTransfer->getPayment()->setPaymentSelection(ComputopConfig::PAYMENT_METHOD_PAY_PAL_EXPRESS);
147
148
        $handler = (new ComputopFactory())->createComputopPaymentHandler();
149
150
        return $handler->addPaymentToQuote($quoteTransfer);
151
    }
152
}
153