Passed
Pull Request — master (#57)
by Ihor
33:01 queued 22:17
created

getCheckoutRedirectUrlFromAuthorizeOnRegistrationTransactionLog()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A BaseHeidelpayPaymentMethod::findOrderAuthorizeTransactionLog() 0 3 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Heidelpay\Business\Payment;
9
10
use Generated\Shared\Transfer\CheckoutResponseTransfer;
11
use Generated\Shared\Transfer\HeidelpayTransactionLogTransfer;
12
use SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface;
13
use SprykerEco\Zed\Heidelpay\HeidelpayConfig;
14
15
class BaseHeidelpayPaymentMethod
16
{
17
    /**
18
     * @var \SprykerEco\Zed\Heidelpay\HeidelpayConfig
19
     */
20
    protected $config;
21
22
    /**
23
     * @var \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface
24
     */
25
    protected $transactionLogManager;
26
27
    /**
28
     * @param \SprykerEco\Zed\Heidelpay\Business\Payment\Transaction\TransactionLogReaderInterface $transactionLogManager
29
     * @param \SprykerEco\Zed\Heidelpay\HeidelpayConfig $config
30
     */
31
    public function __construct(
32
        TransactionLogReaderInterface $transactionLogManager,
33
        HeidelpayConfig $config
34
    ) {
35
        $this->transactionLogManager = $transactionLogManager;
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @param int $idSalesOrder
41
     *
42
     * @return string
43
     */
44
    protected function getCheckoutRedirectUrlFromAuthorizeTransactionLog(int $idSalesOrder): string
45
    {
46
        $authorizeTransactionLogTransfer = $this->findOrderAuthorizeTransactionLog($idSalesOrder);
47
48
        if (
49
            ($authorizeTransactionLogTransfer !== null) &&
50
            ($this->isAuthorizeTransactionSentSuccessfully($authorizeTransactionLogTransfer))
51
        ) {
52
            return $this->getAuthorizeRedirectUrl($authorizeTransactionLogTransfer);
53
        }
54
55
        return $this->getAuthorizeFailedRedirectUrl($authorizeTransactionLogTransfer);
56
    }
57
58
    /**
59
     * @param int $idSalesOrder
60
     *
61
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
62
     */
63
    protected function findOrderAuthorizeTransactionLog(int $idSalesOrder): ?HeidelpayTransactionLogTransfer
64
    {
65
        return $this->transactionLogManager->findOrderAuthorizeTransactionLogByIdSalesOrder($idSalesOrder);
66
    }
67
68
    /**
69
     * @param int $idSalesOrder
70
     *
71
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
72
     */
73
    protected function findOrderAuthorizeOnRegistrationTransactionLog($idSalesOrder)
74
    {
75
        return $this->transactionLogManager->findOrderAuthorizeOnRegistrationTransactionLogByIdSalesOrder($idSalesOrder);
76
    }
77
78
    /**
79
     * @param string $redirectUrl
80
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
81
     *
82
     * @return void
83
     */
84
    protected function setExternalRedirect(string $redirectUrl, CheckoutResponseTransfer $checkoutResponseTransfer): void
85
    {
86
        $checkoutResponseTransfer->setIsExternalRedirect(true);
87
        $checkoutResponseTransfer->setRedirectUrl($redirectUrl);
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer $authorizeTransactionLogTransfer
92
     *
93
     * @return bool
94
     */
95
    protected function isAuthorizeTransactionSentSuccessfully(HeidelpayTransactionLogTransfer $authorizeTransactionLogTransfer): bool
96
    {
97
        return $authorizeTransactionLogTransfer->getHeidelpayResponse()->getIsSuccess();
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer $transactionLogTransfer
102
     *
103
     * @return string
104
     */
105
    protected function getAuthorizeRedirectUrl(HeidelpayTransactionLogTransfer $transactionLogTransfer): string
106
    {
107
        return $transactionLogTransfer->getHeidelpayResponse()->getPaymentFormUrl();
108
    }
109
110
    /**
111
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null $transactionLogTransfer
112
     *
113
     * @return string
114
     */
115
    protected function getAuthorizeFailedRedirectUrl(?HeidelpayTransactionLogTransfer $transactionLogTransfer = null): string
116
    {
117
        $paymentFailedUrl = $this->config->getYvesCheckoutPaymentFailedUrl();
118
119
        if ($transactionLogTransfer === null) {
120
            return sprintf($paymentFailedUrl, '');
121
        }
122
123
        $errorCode = $transactionLogTransfer->getHeidelpayResponse()->getError()->getCode();
124
125
        return sprintf($paymentFailedUrl, $errorCode);
126
    }
127
}
128