Test Failed
Push — master ( 2c296d...48b8fb )
by Adriano
02:02
created

CepLa::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Wead\ZipCode\WS;
4
5
use GuzzleHttp\Client;
6
7
class CepLa
8
{
9
    private $endPoint = "http://cep.la";
10
11
    public function getAddressFromZipcode($zipCode)
12
    {
13
        $zipCode = preg_replace('/[^0-9]/im', '', $zipCode);
14
15
        $headers = [
16
            "Accept" => "application/json",
17
        ];
18
19
        $client = new Client(['base_uri' => "{$this->endPoint}/{$zipCode}"]);
20
21
        $response = $client->get(
22
            '',
23
            [
24
                'headers' => $headers,
25
                'connect_timeout' => 5, // seconds
26
                'debug' => false,
27
                ]
28
        );
29
30
        $response = $response->getBody()->getContents();
31
        $response = json_decode($response);
32
33
        return $this->normalizeResponse((array)$response);
34
    }
35
36
    private function normalizeResponse($address)
37
    {
38
        if (sizeof($address) > 0) {
39
            return [
40
                "status" => true,
41
                "address" => $address["logradouro"],
42
                "district" => $address["bairro"],
43
                "city" => $address["cidade"],
44
                "state" => $address["uf"],
45
                "api" => "CepLa"
46
            ];
47
        } else {
48
            return [
49
                "status" => false,
50
                "address" => null,
51
                "district" => null,
52
                "city" => null,
53
                "state" => null,
54
                "api" => "CepLa"
55
            ];
56
        }
57
    }
58
}
59