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\ErrorResponseException; |
8
|
|
|
use Cakasim\Payone\Sdk\Api\Format\DecoderExceptionInterface; |
9
|
|
|
use Cakasim\Payone\Sdk\Api\Format\DecoderInterface; |
10
|
|
|
use Cakasim\Payone\Sdk\Api\Message\BinaryResponseInterface; |
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 binary response helper handles the response as |
17
|
|
|
* binary data like PDF files. |
18
|
|
|
* |
19
|
|
|
* @author Fabian Böttcher <[email protected]> |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
*/ |
22
|
|
|
class BinaryResponseHelper implements ResponseHelperInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DecoderInterface The API format decoder. |
26
|
|
|
*/ |
27
|
|
|
protected $decoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructs the response helper for binary responses. |
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 $response instanceof BinaryResponseInterface; |
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
|
|
|
$bodySize = $response->getBody()->getSize(); |
61
|
|
|
|
62
|
|
|
// Check for error if an error response (parameter format) is plausible. |
63
|
|
|
if ($bodySize !== null && $bodySize < 1280) { |
64
|
|
|
$this->checkForError($response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $response->getBody(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Checks if the response data is a PAYONE API error in parameter format. |
72
|
|
|
* |
73
|
|
|
* @param HttpResponseInterface $response The HTTP response. |
74
|
|
|
* @throws ErrorResponseException If the response data is a PAYONE API error. |
75
|
|
|
*/ |
76
|
|
|
protected function checkForError(HttpResponseInterface $response): void |
77
|
|
|
{ |
78
|
|
|
// Get whole body contents of the response. |
79
|
|
|
$bodyContents = $response->getBody()->getContents(); |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
// Try to decode the response body to parameter array. |
83
|
|
|
$responseParameters = $this->decoder->decode($bodyContents); |
84
|
|
|
|
85
|
|
|
if (($responseParameters['status'] ?? null) === 'ERROR') { |
86
|
|
|
throw new ErrorResponseException( |
87
|
|
|
(int) ($responseParameters['errorcode'] ?? 0), |
88
|
|
|
$responseParameters['errormessage'] ?? '', |
89
|
|
|
$responseParameters['customermessage'] ?? '' |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} catch (DecoderExceptionInterface $e) { |
93
|
|
|
// Ignore any decoder exceptions because we should assume |
94
|
|
|
// that the response data is actually just binary data. |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|