HeidelpayController::getCustomerRedirectUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Yves\Heidelpay\Controller;
9
10
use Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\StreamedResponse;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16
/**
17
 * @method \SprykerEco\Yves\Heidelpay\HeidelpayFactory getFactory()
18
 * @method \SprykerEco\Client\Heidelpay\HeidelpayClientInterface getClient()
19
 */
20
class HeidelpayController extends BaseHeidelpayController
21
{
22
    public const PARAM_ERROR_CODE = 'error_code';
23
24
    /**
25
     * @param \Symfony\Component\HttpFoundation\Request $request
26
     *
27
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
28
     */
29
    public function paymentFailedAction(Request $request): RedirectResponse
30
    {
31
        $errorCode = $request->get(static::PARAM_ERROR_CODE, '');
32
33
        $redirectResponseWithErrorTransfer = $this->getFactory()
34
            ->createPaymentFailureHandler()
35
            ->handlePaymentFailureByErrorCode($errorCode);
36
37
        $this->addErrorMessage($redirectResponseWithErrorTransfer->getErrorMessage());
38
39
        return new RedirectResponse($redirectResponseWithErrorTransfer->getRedirectUrl());
40
    }
41
42
    /**
43
     * @param \Symfony\Component\HttpFoundation\Request $request
44
     *
45
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
46
     *
47
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
48
     */
49
    public function paymentAction(Request $request): StreamedResponse
50
    {
51
        if (!$request->isMethod(Request::METHOD_POST)) {
52
            throw new NotFoundHttpException();
53
        }
54
55
        $processingResultTransfer = $this->getFactory()
56
            ->createHeidelpayPaymentResponseProcessor()
57
            ->processPaymentResponse($request);
58
59
        return $this->streamResponse($processingResultTransfer);
60
    }
61
62
    /**
63
     * @param \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer
64
     *
65
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
66
     */
67
    protected function streamResponse(HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer): StreamedResponse
68
    {
69
        $callback = function () use ($processingResultTransfer) {
70
            echo $this->getCustomerRedirectUrl($processingResultTransfer);
71
        };
72
73
        return $this->streamedResponse($callback);
74
    }
75
76
    /**
77
     * @param \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer
78
     *
79
     * @return string
80
     */
81
    protected function getCustomerRedirectUrl(HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer): string
82
    {
83
        return $processingResultTransfer->getIsError()
84
            ? $this->getFailureRedirectUrl($processingResultTransfer)
85
            : $this->getSuccessRedirectUrl();
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    protected function getSuccessRedirectUrl(): string
92
    {
93
        return $this->getConfig()->getYvesCheckoutSuccessUrl();
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer
98
     *
99
     * @return string
100
     */
101
    protected function getFailureRedirectUrl(HeidelpayPaymentProcessingResponseTransfer $processingResultTransfer): string
102
    {
103
        return sprintf(
104
            $this->getConfig()->getYvesCheckoutPaymentFailedUrl(),
105
            $processingResultTransfer->getError()->getCode()
106
        );
107
    }
108
}
109