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

WideNet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
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 WideNet
8
{
9
    private $endPoint = "http://apps.widenet.com.br/busca-cep/api/cep.json";
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]);
20
21
        $response = $client->get(
22
            '',
23
            [
24
                'headers' => $headers,
25
                'connect_timeout' => 5, // seconds
26
                'query' => [
27
                    'code' => $zipCode
28
                ],
29
                'debug' => false
30
            ]
31
        );
32
33
        $response = $response->getBody()->getContents();
34
        $response = json_decode($response);
35
36
        return $this->normalizeResponse((array)$response);
37
    }
38
39
    private function normalizeResponse($address)
40
    {
41
        if (sizeof($address) > 0 && isset($address["address"])) {
42
            return [
43
                "status" => true,
44
                "address" => $address["address"],
45
                "district" => $address["district"],
46
                "city" => $address["city"],
47
                "state" => $address["state"],
48
                "api" => "WideNet"
49
            ];
50
        } else {
51
            return [
52
                "status" => false,
53
                "address" => null,
54
                "district" => null,
55
                "city" => null,
56
                "state" => null,
57
                "api" => "WideNet"
58
            ];
59
        }
60
    }
61
}
62