|
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\Zed\CrefoPayApi\Business\Response\Converter; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\CrefoPayApiErrorResponseTransfer; |
|
|
|
|
|
|
11
|
|
|
use Generated\Shared\Transfer\CrefoPayApiResponseTransfer; |
|
|
|
|
|
|
12
|
|
|
use SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface; |
|
13
|
|
|
use SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig; |
|
14
|
|
|
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface; |
|
15
|
|
|
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface; |
|
16
|
|
|
|
|
17
|
|
|
class CrefoPayApiResponseConverter implements CrefoPayApiResponseConverterInterface |
|
18
|
|
|
{ |
|
19
|
|
|
protected const EXTERNAL_ERROR_MESSAGE = 'CrefoPay service temporarily unavailable.'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $encodingService; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $responseMapper; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService |
|
38
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface $responseMapper |
|
39
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
CrefoPayApiToUtilEncodingServiceInterface $encodingService, |
|
43
|
|
|
CrefoPayApiResponseMapperInterface $responseMapper, |
|
44
|
|
|
CrefoPayApiConfig $config |
|
45
|
|
|
) { |
|
46
|
|
|
$this->encodingService = $encodingService; |
|
47
|
|
|
$this->responseMapper = $responseMapper; |
|
48
|
|
|
$this->config = $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response |
|
53
|
|
|
* @param bool $isSuccess |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer |
|
56
|
|
|
*/ |
|
57
|
|
|
public function convertToResponseTransfer( |
|
58
|
|
|
CrefoPayApiGuzzleResponseInterface $response, |
|
59
|
|
|
bool $isSuccess = true |
|
60
|
|
|
): CrefoPayApiResponseTransfer { |
|
61
|
|
|
$responseData = $this->encodingService->decodeJson($response->getResponseBody(), true); |
|
62
|
|
|
|
|
63
|
|
|
if (!$isSuccess || !$this->isResultCodeSuccess($responseData)) { |
|
|
|
|
|
|
64
|
|
|
return $this->createResponseTransferWithError($responseData); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$responseTransfer = $this->createSuccessResponseTransfer(); |
|
68
|
|
|
$responseTransfer = $this->responseMapper |
|
69
|
|
|
->mapResponseDataToResponseTransfer($responseData, $responseTransfer); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $responseTransfer; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $responseData |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function isResultCodeSuccess(array $responseData): bool |
|
80
|
|
|
{ |
|
81
|
|
|
if ($responseData === null) { |
|
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$resultCode = $responseData[$this->config->getApiResponseFieldResultCode()]; |
|
86
|
|
|
|
|
87
|
|
|
return isset($resultCode) && ($resultCode === 0 || $resultCode === 1); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array|null $responseData |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function createResponseTransferWithError(?array $responseData): CrefoPayApiResponseTransfer |
|
96
|
|
|
{ |
|
97
|
|
|
$errorTransfer = (new CrefoPayApiErrorResponseTransfer()) |
|
98
|
|
|
->setMessage(static::EXTERNAL_ERROR_MESSAGE) |
|
99
|
|
|
->setErrorType($this->config->getApiErrorTypeExternal()); |
|
100
|
|
|
|
|
101
|
|
|
if ($responseData !== null) { |
|
|
|
|
|
|
102
|
|
|
$errorTransfer->fromArray($responseData, true); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return (new CrefoPayApiResponseTransfer()) |
|
106
|
|
|
->setIsSuccess(false) |
|
107
|
|
|
->setError($errorTransfer); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function createSuccessResponseTransfer(): CrefoPayApiResponseTransfer |
|
114
|
|
|
{ |
|
115
|
|
|
return (new CrefoPayApiResponseTransfer()) |
|
116
|
|
|
->setIsSuccess(true); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths