1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Gateway Trait |
6
|
|
|
* @category Ticaje |
7
|
|
|
* @author Max Demian <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ticaje\AConnector\Traits\Gateway\Client; |
11
|
|
|
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
|
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
use Ticaje\AConnector\Interfaces\Protocol\RestClientInterface; |
16
|
|
|
use Ticaje\Contract\Patterns\Interfaces\Decorator\ResponderInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait Rest |
20
|
|
|
* @package Ticaje\AConnector\Traits\Gateway\Client |
21
|
|
|
* This trait prevents code duplication at the time that fits right with service contracts |
22
|
|
|
* Uses design-by-contract pattern in order to guarantee business rules |
23
|
|
|
*/ |
24
|
|
|
trait Rest |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param $verb |
28
|
|
|
* @param $endpoint |
29
|
|
|
* @param array $headers |
30
|
|
|
* @param array $params |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function requestAsync($verb, $endpoint, array $headers = [], array $params) |
34
|
|
|
{ |
35
|
|
|
$result = []; |
36
|
|
|
try { |
37
|
|
|
$promise = $this->client->requestAsync( |
38
|
|
|
$verb, |
39
|
|
|
$endpoint, |
40
|
|
|
[ |
41
|
|
|
'headers' => $this->generateHeaders($headers), |
|
|
|
|
42
|
|
|
$this->getFormRequestKey($verb, $headers) => $params |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
$promise->then( |
46
|
|
|
function (ResponseInterface $response) use (&$result) { |
47
|
|
|
$result[ResponderInterface::CONTENT_KEY] = $response->getBody()->getContents(); |
48
|
|
|
$result[ResponderInterface::SUCCESS_KEY] = true; |
49
|
|
|
$result[ResponderInterface::MESSAGE_KEY] = 'Response correctly received'; // Perhaps normalize this message |
50
|
|
|
return $result; |
51
|
|
|
}, |
52
|
|
|
function (RuntimeException $exception) { |
|
|
|
|
53
|
|
|
// Missing implementation |
54
|
|
|
} |
55
|
|
|
); |
56
|
|
|
$promise->wait(); |
57
|
|
|
} catch (\Exception $exception) { |
58
|
|
|
$result[ResponderInterface::CONTENT_KEY] = null; |
59
|
|
|
$result[ResponderInterface::SUCCESS_KEY] = false; |
60
|
|
|
$result[ResponderInterface::MESSAGE_KEY] = $exception->getMessage(); |
61
|
|
|
} |
62
|
|
|
return $this->responder->process($result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $verb |
67
|
|
|
* @param $endpoint |
68
|
|
|
* @param array $headers |
69
|
|
|
* @param array $params |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function request($verb, $endpoint, array $headers = [], array $params = []) |
73
|
|
|
{ |
74
|
|
|
$result = []; |
75
|
|
|
try { |
76
|
|
|
$result[ResponderInterface::CONTENT_KEY] = $this->client->request( |
77
|
|
|
$verb, |
78
|
|
|
$endpoint, |
79
|
|
|
[ |
80
|
|
|
'headers' => $this->generateHeaders($headers), |
81
|
|
|
$this->getFormRequestKey($verb, $headers) => $params |
82
|
|
|
] |
83
|
|
|
)->getBody()->getContents(); |
84
|
|
|
$result[ResponderInterface::SUCCESS_KEY] = true; |
85
|
|
|
$result[ResponderInterface::MESSAGE_KEY] = 'Response correctly received'; // Perhaps normalize this message |
86
|
|
|
} catch (\Exception $exception) { |
87
|
|
|
$result[ResponderInterface::CONTENT_KEY] = null; |
88
|
|
|
$result[ResponderInterface::SUCCESS_KEY] = false; |
89
|
|
|
$result[ResponderInterface::MESSAGE_KEY] = $exception->getMessage(); |
90
|
|
|
} |
91
|
|
|
return $this->responder->process($result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $verb |
96
|
|
|
* @param $headers |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function getFormRequestKey($verb, $headers) |
100
|
|
|
{ |
101
|
|
|
// The values should also be constantized |
102
|
|
|
$key = $verb ? 'query' : 'form_params'; |
103
|
|
|
$key = $headers[RestClientInterface::CONTENT_TYPE_KEY] == RestClientInterface::CONTENT_TYPE_JSON ? 'json' : $key; |
104
|
|
|
return $key; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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