|
1
|
|
|
<?php |
|
2
|
|
|
namespace Integracao\ControlPay; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp; |
|
5
|
|
|
use Integracao\ControlPay\Constants\ControlPayParameterConst; |
|
6
|
|
|
use Integracao\ControlPay\Impl\BasicAuthentication; |
|
7
|
|
|
use Integracao\ControlPay\Impl\KeyQueryStringAuthentication; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractAPI |
|
11
|
|
|
* @package Integracao\NTKPedido |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractAPI |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Client |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $_client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var GuzzleHttp\Client |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $_httpClient; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $url; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $endpoint; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var GuzzleHttp\Query |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $query; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var GuzzleHttp\Message\ResponseInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $response; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $headers = [ |
|
49
|
|
|
'Content-Type' => 'application/json; charset=utf-8', |
|
50
|
|
|
'Cache-Control' => 'no-cache', |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* AbstractAPI constructor. |
|
55
|
|
|
* @param $endpoint |
|
56
|
|
|
* @param Client|null $client |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function __construct($endpoint, Client $client = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->endpoint = $endpoint; |
|
61
|
|
|
$this->_client = $client; |
|
62
|
|
|
|
|
63
|
|
|
if(is_null($this->_client)) |
|
64
|
|
|
$this->_client = new Client(); |
|
65
|
|
|
|
|
66
|
|
|
$this->query = new GuzzleHttp\Query(); |
|
67
|
|
|
$this->query->setEncodingType(false); |
|
68
|
|
|
|
|
69
|
|
|
switch ($this->_client->getParameter(ControlPayParameterConst::CONTROLPAY_OAUTH_TYPE)) |
|
70
|
|
|
{ |
|
71
|
|
|
case KeyQueryStringAuthentication::class: |
|
72
|
|
|
$this->query->set('key', $this->_client->getAuthentication()); |
|
73
|
|
|
break; |
|
74
|
|
|
case BasicAuthentication::class: |
|
75
|
|
|
$this->headers['Authorization'] = $this->_client->getAuthentication(); |
|
76
|
|
|
break; |
|
77
|
|
|
default: |
|
78
|
|
|
$this->query->set('key', $this->_client->getAuthentication()); |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->_httpClient = new GuzzleHttp\Client([ |
|
83
|
|
|
'base_url' => sprintf("%s/%s/", |
|
84
|
|
|
$client->getParameter(ControlPayParameterConst::CONTROLPAY_HOST), |
|
|
|
|
|
|
85
|
|
|
$this->endpoint |
|
86
|
|
|
), |
|
87
|
|
|
'timeout' => $client->getParameter(ControlPayParameterConst::CONTROLPAY_TIMEOUT), |
|
88
|
|
|
'verify' => false, |
|
89
|
|
|
'defaults' => [ |
|
90
|
|
|
'headers' => $this->headers, |
|
91
|
|
|
'query' => $this->query |
|
92
|
|
|
] |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Adiciona parâmetros mantendo os já inicializados como padrão |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $params |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function addQueryAdditionalParameters(array $params) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->query = new GuzzleHttp\Query(); |
|
105
|
|
|
|
|
106
|
|
|
if(isset($this->_httpClient->getDefaultOption()['query'])) |
|
107
|
|
|
$this->query = $this->_httpClient->getDefaultOption()['query']; |
|
108
|
|
|
|
|
109
|
|
|
foreach ($params as $key => $value) |
|
110
|
|
|
$this->query->set($key, $value); |
|
111
|
|
|
|
|
112
|
|
|
return $this->query; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return GuzzleHttp\Message\ResponseInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getResponse() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->response; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getQueryParameters() |
|
127
|
|
|
{ |
|
128
|
|
|
return isset($this->_httpClient->getDefaultOption()['query']) ? |
|
129
|
|
|
$this->_httpClient->getDefaultOption()['query'] : []; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getHeaders() |
|
136
|
|
|
{ |
|
137
|
|
|
return isset($this->_httpClient->getDefaultOption()['headers']) ? |
|
138
|
|
|
$this->_httpClient->getDefaultOption()['headers'] : []; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: