parseResponse()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 3
nop 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\Client\Heidelpay\Sdk;
9
10
use Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer;
11
use Generated\Shared\Transfer\HeidelpayResponseErrorTransfer;
12
use Heidelpay\PhpPaymentApi\Exceptions\HashVerificationException;
13
use Heidelpay\PhpPaymentApi\Response;
14
use SprykerEco\Client\Heidelpay\HeidelpayConfig;
15
use SprykerEco\Client\Heidelpay\Mapper\DirectDebitRegistrationResponseMapperInterface;
16
17
class DirectDebitRegistrationResponseParser implements DirectDebitRegistrationResponseParserInterface
18
{
19
    public const ERROR_CODE_INVALID_RESPONSE = 'invalid-response';
20
21
    /**
22
     * @var \SprykerEco\Client\Heidelpay\Mapper\DirectDebitRegistrationResponseMapperInterface
23
     */
24
    protected $registrationResponseMapper;
25
26
    /**
27
     * @var \SprykerEco\Client\Heidelpay\HeidelpayConfig
28
     */
29
    protected $config;
30
31
    /**
32
     * @param \SprykerEco\Client\Heidelpay\Mapper\DirectDebitRegistrationResponseMapperInterface $registrationResponseMapper
33
     * @param \SprykerEco\Client\Heidelpay\HeidelpayConfig $config
34
     */
35
    public function __construct(
36
        DirectDebitRegistrationResponseMapperInterface $registrationResponseMapper,
37
        HeidelpayConfig $config
38
    ) {
39
        $this->registrationResponseMapper = $registrationResponseMapper;
40
        $this->config = $config;
41
    }
42
43
    /**
44
     * @param string[] $responseArray
45
     *
46
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
47
     */
48
    public function parseResponse(array $responseArray): HeidelpayDirectDebitRegistrationTransfer
49
    {
50
        try {
51
            $apiResponseObject = $this->getValidatedApiResponseObject($responseArray);
52
53
            return $this->createDirectDebitRegistrationResponseTransfer($apiResponseObject);
54
        } catch (HashVerificationException $exception) {
55
            return $this->createDirectDebitRegistrationResponseTransferWithError();
56
        }
57
    }
58
59
    /**
60
     * @param string[] $apiResponseArray
61
     *
62
     * @return \Heidelpay\PhpPaymentApi\Response
63
     */
64
    protected function getValidatedApiResponseObject(array $apiResponseArray): Response
65
    {
66
        $apiResponse = new Response($apiResponseArray);
67
68
        $apiResponse->verifySecurityHash(
69
            $this->config->getApplicationSecret(),
70
            $apiResponse->getIdentification()->getTransactionId()
71
        );
72
73
        return $apiResponse;
74
    }
75
76
    /**
77
     * @param \Heidelpay\PhpPaymentApi\Response $apiResponseObject
78
     *
79
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
80
     */
81
    protected function createDirectDebitRegistrationResponseTransfer(
82
        Response $apiResponseObject
83
    ): HeidelpayDirectDebitRegistrationTransfer {
84
        return $this->registrationResponseMapper
85
            ->mapApiResponseToDirectDebitRegistrationResponseTransfer(
86
                $apiResponseObject,
87
                new HeidelpayDirectDebitRegistrationTransfer()
88
            );
89
    }
90
91
    /**
92
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
93
     */
94
    protected function createDirectDebitRegistrationResponseTransferWithError(): HeidelpayDirectDebitRegistrationTransfer
95
    {
96
        return (new HeidelpayDirectDebitRegistrationTransfer())
97
            ->setIsError(true)
98
            ->setError(
99
                (new HeidelpayResponseErrorTransfer())
100
                    ->setCode(static::ERROR_CODE_INVALID_RESPONSE)
101
            );
102
    }
103
}
104