Completed
Push — develop ( 97a737...3a270c )
by Fabian
29s queued 18s
created

DefaultResponseHelper::makeResponseData()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 32
rs 9.4555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Api\Client\ResponseHelper;
6
7
use Cakasim\Payone\Sdk\Api\Client\ClientException;
8
use Cakasim\Payone\Sdk\Api\Client\ErrorResponseException;
9
use Cakasim\Payone\Sdk\Api\Format\DecoderExceptionInterface;
10
use Cakasim\Payone\Sdk\Api\Format\DecoderInterface;
11
use Cakasim\Payone\Sdk\Api\Message\ResponseInterface;
12
use Psr\Http\Message\RequestInterface as HttpRequestInterface;
13
use Psr\Http\Message\ResponseInterface as HttpResponseInterface;
14
15
/**
16
 * The default response helper handles the response as
17
 * regular PAYONE API parameters.
18
 *
19
 * @author Fabian Böttcher <[email protected]>
20
 * @since 0.1.0
21
 */
22
class DefaultResponseHelper implements ResponseHelperInterface
23
{
24
    /**
25
     * @var DecoderInterface The API format decoder.
26
     */
27
    protected $decoder;
28
29
    /**
30
     * Constructs the default response helper.
31
     *
32
     * @param DecoderInterface $decoder The API format decoder.
33
     */
34
    public function __construct(DecoderInterface $decoder)
35
    {
36
        $this->decoder = $decoder;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function isResponsible(ResponseInterface $response): bool
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function modifyHttpRequest(HttpRequestInterface $request): HttpRequestInterface
51
    {
52
        return $request;
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function makeResponseData(HttpResponseInterface $response)
59
    {
60
        // Ensure text/plain content type which indicates a parameter response.
61
        if (stristr($response->getHeaderLine('Content-Type'), 'text/plain') === false) {
62
            throw new ClientException("Cannot read response parameters. The 'Content-Type' response header is not 'text/plain'.");
63
        }
64
65
        // Get whole body contents of the response.
66
        $bodyContents = $response->getBody()->getContents();
67
68
        // Expect a non-empty response body.
69
        if (empty($bodyContents)) {
70
            throw new ClientException("Cannot read response parameters. The response body is empty.");
71
        }
72
73
        try {
74
            // Decode response body to parameter array.
75
            $responseParameters = $this->decoder->decode($bodyContents);
76
        } catch (DecoderExceptionInterface $e) {
77
            throw new ClientException("Cannot read response parameters. Failed decoding the response body.", 0, $e);
78
        }
79
80
        // Check for API error response, handle missing status parameter as error.
81
        if (($responseParameters['status'] ?? 'ERROR') === 'ERROR') {
82
            throw new ErrorResponseException(
83
                (int) ($responseParameters['errorcode'] ?? 0),
84
                $responseParameters['errormessage'] ?? '',
85
                $responseParameters['customermessage'] ?? ''
86
            );
87
        }
88
89
        return $responseParameters;
90
    }
91
}
92