Passed
Push — fix/eco-1986-migrate-to-latest... ( 4c2f96...390bc6 )
by Andrey
05:15
created

WidgetController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A walletWidgetAction() 0 8 2
A isAmazonPaymentInvalid() 0 8 3
A isLogout() 0 13 4
A getAmazonPayConfig() 0 3 1
A getAmazonPaymentOrderReferenceId() 0 7 3
A checkoutWidgetAction() 0 20 3
A resetAmazonPaymentInQuote() 0 8 1
A payButtonAction() 0 15 3
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\AmazonPay\Controller;
9
10
use Generated\Shared\Transfer\QuoteTransfer;
1 ignored issue
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...
11
use Spryker\Yves\Kernel\Controller\AbstractController;
12
use SprykerEco\Shared\AmazonPay\AmazonPayConfig;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * @method \SprykerEco\Yves\AmazonPay\AmazonPayFactory getFactory()
18
 */
19
class WidgetController extends AbstractController
20
{
21
    const ADDRESS_BOOK_MODE = 'addressBookMode';
22
    const AMAZON_PAY_CONFIG = 'amazonpayConfig';
23
    const LOGOUT = 'logout';
24
    const ORDER_REFERENCE = 'orderReferenceId';
25
26
    /**
27
     * @param \Symfony\Component\HttpFoundation\Request $request
28
     *
29
     * @return \Symfony\Component\HttpFoundation\Response|array
30
     */
31
    public function payButtonAction(Request $request)
32
    {
33
        if (!$request->isSecure()) {
34
            return new Response('');
35
        }
36
37
        $isLogout = $this->isLogout();
38
39
        if ($isLogout) {
40
            $this->resetAmazonPaymentInQuote();
41
        }
42
43
        return [
44
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
45
            static::LOGOUT => $isLogout,
46
        ];
47
    }
48
49
    /**
50
     * @return void
51
     */
52
    protected function resetAmazonPaymentInQuote()
53
    {
54
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
55
56
        $quoteTransfer->setAmazonpayPayment(null);
57
        $this->getFactory()
58
            ->getQuoteClient()
59
            ->setQuote($quoteTransfer);
60
    }
61
62
    /**
63
     * @param \Symfony\Component\HttpFoundation\Request $request
64
     *
65
     * @return \Symfony\Component\HttpFoundation\Response|array
66
     */
67
    public function checkoutWidgetAction(Request $request)
68
    {
69
        if (!$request->isSecure()) {
70
            return new Response('');
71
        }
72
73
        $quoteTransfer = $this->getFactory()
74
            ->getQuoteClient()
75
            ->getQuote();
76
77
        $data = [
78
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
79
        ];
80
81
        if ($this->isAmazonPaymentInvalid($quoteTransfer)) {
82
            $data[static::ORDER_REFERENCE] = $this->getAmazonPaymentOrderReferenceId($quoteTransfer);
83
            $data[static::ADDRESS_BOOK_MODE] = AmazonPayConfig::DISPLAY_MODE_READONLY;
84
        }
85
86
        return $data;
87
    }
88
89
    /**
90
     * @param \Symfony\Component\HttpFoundation\Request $request
91
     *
92
     * @return \Symfony\Component\HttpFoundation\Response|array
93
     */
94
    public function walletWidgetAction(Request $request)
95
    {
96
        if (!$request->isSecure()) {
97
            return new Response('');
98
        }
99
100
        return [
101
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
102
        ];
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    protected function isLogout()
109
    {
110
        $quote = $this->getFactory()->getQuoteClient()->getQuote();
111
112
        $isLogout = $quote->getAmazonpayPayment()
113
            && $quote->getAmazonpayPayment()->getResponseHeader()
114
            && !$quote->getAmazonpayPayment()->getResponseHeader()->getIsSuccess();
115
116
        if ($isLogout) {
117
            return 1;
118
        }
119
120
        return 0;
121
    }
122
123
    /**
124
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
125
     *
126
     * @return null|string
127
     */
128
    protected function getAmazonPaymentOrderReferenceId(QuoteTransfer $quoteTransfer)
129
    {
130
        if ($quoteTransfer->getAmazonpayPayment() !== null && $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId() !== null) {
131
            return $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId();
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * @return \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface
139
     */
140
    protected function getAmazonPayConfig()
141
    {
142
        return $this->getFactory()->createAmazonPayConfig();
143
    }
144
145
    /**
146
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
147
     *
148
     * @return bool
149
     */
150
    protected function isAmazonPaymentInvalid(QuoteTransfer $quoteTransfer)
151
    {
152
        if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader() !== null
153
            && $quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsInvalidPaymentMethod()) {
154
            return true;
155
        }
156
157
        return false;
158
    }
159
}
160