1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © O2TI. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* @author Bruno Elisei <[email protected]> |
6
|
|
|
* See COPYING.txt for license details. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace O2TI\AutoCompleteAddressBr\Controller\Postcode; |
10
|
|
|
|
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use Magento\Directory\Model\Region; |
13
|
|
|
use Magento\Framework\App\Action\Action; |
14
|
|
|
use Magento\Framework\App\Action\Context; |
15
|
|
|
use Magento\Framework\App\Action\HttpGetActionInterface; |
16
|
|
|
use Magento\Framework\Controller\Result\JsonFactory; |
17
|
|
|
use Magento\Framework\HTTP\ZendClient; |
18
|
|
|
use Magento\Framework\HTTP\ZendClientFactory; |
|
|
|
|
19
|
|
|
use Magento\Framework\Serialize\Serializer\Json; |
20
|
|
|
use O2TI\AutoCompleteAddressBr\Helper\Config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Controller Address - Complete Address by API. |
24
|
|
|
*/ |
25
|
|
|
class Address extends Action implements HttpGetActionInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ZendClientFactory |
29
|
|
|
*/ |
30
|
|
|
protected $httpClientFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var JsonFactory |
34
|
|
|
*/ |
35
|
|
|
protected $resultJsonFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Region |
39
|
|
|
*/ |
40
|
|
|
protected $region; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Json |
44
|
|
|
*/ |
45
|
|
|
protected $json; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Config |
49
|
|
|
*/ |
50
|
|
|
protected $config; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Context $context |
54
|
|
|
* @param ZendClientFactory $httpClientFactory |
55
|
|
|
* @param JsonFactory $resultJsonFactory |
56
|
|
|
* @param Region $region |
57
|
|
|
* @param Json $json |
58
|
|
|
* @param Config $config |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
Context $context, |
62
|
|
|
ZendClientFactory $httpClientFactory, |
63
|
|
|
JsonFactory $resultJsonFactory, |
64
|
|
|
Region $region, |
65
|
|
|
Json $json, |
66
|
|
|
Config $config |
67
|
|
|
) { |
68
|
|
|
$this->httpClientFactory = $httpClientFactory; |
69
|
|
|
$this->resultJsonFactory = $resultJsonFactory; |
70
|
|
|
$this->region = $region; |
71
|
|
|
$this->json = $json; |
72
|
|
|
$this->config = $config; |
73
|
|
|
parent::__construct($context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function execute() |
80
|
|
|
{ |
81
|
|
|
$result = ['success'=>false]; |
82
|
|
|
$return = $this->resultJsonFactory->create(); |
83
|
|
|
|
84
|
|
|
if ($zipcode = $this->getRequest()->getParam('zipcode')) { |
85
|
|
|
$zipcode = preg_replace('/[^0-9]/', '', $zipcode); |
86
|
|
|
|
87
|
|
|
$data = $this->getApiServices($zipcode); |
88
|
|
|
|
89
|
|
|
if (!$data['success']) { |
90
|
|
|
return $return->setData($data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result = $this->getApiFormartReturn($data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $return->setData($result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get API. |
101
|
|
|
* |
102
|
|
|
* @param string $zipcode |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getApiServices(string $zipcode): array |
107
|
|
|
{ |
108
|
|
|
$client = $this->httpClientFactory->create(); |
109
|
|
|
$api = $this->config->getConfigForDeveloper('api'); |
110
|
|
|
$url = 'http://endereco.ecorreios.com.br/app/enderecoCep.php?cep='.$zipcode; |
111
|
|
|
|
112
|
|
|
if ($api === 'ecorreios') { |
113
|
|
|
$url = 'http://endereco.ecorreios.com.br/app/enderecoCep.php?cep='.$zipcode; |
114
|
|
|
} elseif ($api === 'viacep') { |
115
|
|
|
$url = 'https://viacep.com.br/ws/'.$zipcode.'/json/'; |
116
|
|
|
} elseif ($api === 'republicavirtual') { |
117
|
|
|
$url = 'http://cep.republicavirtual.com.br/web_cep.php?cep='.$zipcode.'&formato=jsonp'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$result = ['success' => false]; |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$client->setUri($url); |
124
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 120]); |
125
|
|
|
$client->setMethod(ZendClient::GET); |
126
|
|
|
$responseBody = $client->request()->getBody(); |
127
|
|
|
$result = $this->json->unserialize($responseBody); |
128
|
|
|
$result['success'] = true; |
129
|
|
|
} catch (InvalidArgumentException $exc) { |
130
|
|
|
$result['messages'] = $exc->getMessage(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get Format Return API. |
138
|
|
|
* |
139
|
|
|
* @param array $data |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function getApiFormartReturn(array $data): array |
144
|
|
|
{ |
145
|
|
|
$api = $this->config->getConfigForDeveloper('api'); |
146
|
|
|
|
147
|
|
|
if (isset($data['uf'])) { |
148
|
|
|
$region = $this->region->loadByCode($data['uf'], 'BR'); |
149
|
|
|
$regionId = $region->getId(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($api === 'ecorreios') { |
153
|
|
|
$data = $this->getFormatECorreios($data); |
154
|
|
|
} elseif ($api === 'viacep') { |
155
|
|
|
$data = $this->getFormatViaCep($data); |
156
|
|
|
} elseif ($api === 'republicavirtual') { |
157
|
|
|
$data = $this->getFormatRepublicaVirtual($data); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$apiData = [ |
161
|
|
|
'success' => $data['success'], |
162
|
|
|
'street' => $data['street'], |
163
|
|
|
'district' => $data['district'], |
164
|
|
|
'city' => $data['city'], |
165
|
|
|
'uf' => isset($regionId) ? $regionId : '', |
166
|
|
|
'provider' => $this->config->getConfigForDeveloper('api'), |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
$result = $this->getRelationShipReturn($apiData); |
170
|
|
|
|
171
|
|
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get Format Return API ECorreios. |
176
|
|
|
* |
177
|
|
|
* @param array $data |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function getFormatECorreios(array $data): array |
182
|
|
|
{ |
183
|
|
|
$data['street'] = isset($data['logradouro']) ? $data['logradouro'] : ''; |
184
|
|
|
$data['district'] = isset($data['bairro']) ? trim($data['bairro']) : ''; |
185
|
|
|
$data['city'] = isset($data['cidade']) ? $data['cidade'] : ''; |
186
|
|
|
|
187
|
|
|
return $data; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get Format Return API ViaCep. |
192
|
|
|
* |
193
|
|
|
* @param array $data |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function getFormatViaCep(array $data): array |
198
|
|
|
{ |
199
|
|
|
$data['street'] = isset($data['logradouro']) ? $data['logradouro'] : ''; |
200
|
|
|
$data['district'] = isset($data['bairro']) ? trim($data['bairro']) : ''; |
201
|
|
|
$data['city'] = isset($data['localidade']) ? $data['localidade'] : ''; |
202
|
|
|
|
203
|
|
|
return $data; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get Format Return API Republica Virtual. |
208
|
|
|
* |
209
|
|
|
* @param array $data |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function getFormatRepublicaVirtual(array $data): array |
214
|
|
|
{ |
215
|
|
|
$data['street'] = isset($data['logradouro']) ? $data['logradouro'] : ''; |
216
|
|
|
$data['district'] = isset($data['bairro']) ? trim($data['bairro']) : ''; |
217
|
|
|
$data['city'] = isset($data['cidade']) ? $data['cidade'] : ''; |
218
|
|
|
|
219
|
|
|
return $data; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get Return Formated. |
224
|
|
|
* |
225
|
|
|
* @param array $apiData |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function getRelationShipReturn(array $apiData): array |
230
|
|
|
{ |
231
|
|
|
$lineToStreet = $this->config->getConfigForRelationShip('street'); |
232
|
|
|
$lineToDistrict = $this->config->getConfigForRelationShip('district'); |
233
|
|
|
|
234
|
|
|
$data = [ |
235
|
|
|
'success' => $apiData['success'], |
236
|
|
|
'street' => [ |
237
|
|
|
$lineToStreet => $apiData['street'], |
238
|
|
|
$lineToDistrict => $apiData['district'], |
239
|
|
|
], |
240
|
|
|
'city' => $apiData['city'], |
241
|
|
|
'country_id' => 'BR', |
242
|
|
|
'region_id' => $apiData['uf'], |
243
|
|
|
'provider' => $apiData['provider'], |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
return $data; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
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