PaymentFailureHandler::buildRedirectResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
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\Handler;
9
10
use Generated\Shared\Transfer\HeidelpayErrorRedirectResponseTransfer;
11
use SprykerEco\Client\Heidelpay\HeidelpayClientInterface;
12
use SprykerEco\Yves\Heidelpay\HeidelpayConfig;
13
14
class PaymentFailureHandler implements PaymentFailureHandlerInterface
15
{
16
    /**
17
     * @var \SprykerEco\Client\Heidelpay\HeidelpayClientInterface
18
     */
19
    protected $heidelpayClient;
20
21
    /**
22
     * @var \SprykerEco\Yves\Heidelpay\HeidelpayConfig
23
     */
24
    protected $config;
25
26
    /**
27
     * @param \SprykerEco\Client\Heidelpay\HeidelpayClientInterface $heidelpayClient
28
     * @param \SprykerEco\Yves\Heidelpay\HeidelpayConfig $config
29
     */
30
    public function __construct(
31
        HeidelpayClientInterface $heidelpayClient,
32
        HeidelpayConfig $config
33
    ) {
34
        $this->heidelpayClient = $heidelpayClient;
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * @param string $errorCode
40
     *
41
     * @return \Generated\Shared\Transfer\HeidelpayErrorRedirectResponseTransfer
42
     */
43
    public function handlePaymentFailureByErrorCode($errorCode): HeidelpayErrorRedirectResponseTransfer
44
    {
45
        $translatedErrorMessage = $this->getCustomerMessageByErrorCode($errorCode);
46
47
        return $this->buildRedirectResponse($translatedErrorMessage);
48
    }
49
50
    /**
51
     * @param string $errorCode
52
     *
53
     * @return string
54
     */
55
    protected function getCustomerMessageByErrorCode($errorCode): string
56
    {
57
        return $this->heidelpayClient->translateErrorMessageByCode($errorCode);
58
    }
59
60
    /**
61
     * @param string $translatedErrorMessage
62
     *
63
     * @return \Generated\Shared\Transfer\HeidelpayErrorRedirectResponseTransfer
64
     */
65
    protected function buildRedirectResponse($translatedErrorMessage): HeidelpayErrorRedirectResponseTransfer
66
    {
67
        return (new HeidelpayErrorRedirectResponseTransfer())
68
            ->setRedirectUrl($this->config->getYvesCheckoutPaymentStepPath())
69
            ->setErrorMessage($translatedErrorMessage);
70
    }
71
}
72