Completed
Pull Request — dev (#22)
by Andrey
09:46 queued 04:14
created

getAmazonPaymentOrderReferenceId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
14
/**
15
 * @method \SprykerEco\Yves\AmazonPay\AmazonPayFactory getFactory()
16
 */
17
class WidgetController extends AbstractController
18
{
19
    const ADDRESS_BOOK_MODE = 'addressBookMode';
20
    const AMAZON_PAY_CONFIG = 'amazonpayConfig';
21
    const LOGOUT = 'logout';
22
    const ORDER_REFERENCE = 'orderReferenceId';
23
24
    /**
25
     * @return array
26
     */
27
    public function payButtonAction()
28
    {
29
        return [
30
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
31
            static::LOGOUT => (int)$this->isLogout(),
32
        ];
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    protected function isLogout()
39
    {
40
        $quote = $this->getFactory()->getQuoteClient()->getQuote();
41
42
        return $quote->getAmazonpayPayment()
43
            && $quote->getAmazonpayPayment()->getResponseHeader()
44
            && !$quote->getAmazonpayPayment()->getResponseHeader()->getIsSuccess();
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function checkoutWidgetAction()
51
    {
52
        $quoteTransfer = $this->getFactory()
53
            ->getQuoteClient()
54
            ->getQuote();
55
56
        $data = [
57
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
58
        ];
59
60
        if ($this->isAmazonPaymentInvalid($quoteTransfer)) {
61
            $data[static::ORDER_REFERENCE] = $this->getAmazonPaymentOrderReferenceId($quoteTransfer);
62
            $data[static::ADDRESS_BOOK_MODE] = AmazonPayConfig::DISPLAY_MODE_READONLY;
63
        }
64
65
        return $data;
66
    }
67
68
    /**
69
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
70
     *
71
     * @return null|string
72
     */
73
    protected function getAmazonPaymentOrderReferenceId(QuoteTransfer $quoteTransfer)
74
    {
75
        if ($quoteTransfer->getAmazonpayPayment() !== null && $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId() !== null) {
76
            return $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId();
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function walletWidgetAction()
86
    {
87
        return [
88
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
89
        ];
90
    }
91
92
    /**
93
     * @return \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface
94
     */
95
    protected function getAmazonPayConfig()
96
    {
97
        return $this->getFactory()->createAmazonPayConfig();
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
102
     *
103
     * @return bool
104
     */
105
    protected function isAmazonPaymentInvalid(QuoteTransfer $quoteTransfer)
106
    {
107
        if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader() !== null
108
            && $quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsInvalidPaymentMethod()) {
109
            return true;
110
        }
111
112
        return false;
113
    }
114
}
115