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