Test Failed
Push — master ( 0272e0...b7ca35 )
by Adriano
04:06 queued 26s
created

ApiCEP   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddressFromZipcode() 0 11 1
A normalizeResponse() 0 19 3
1
<?php
2
3
namespace Wead\ZipCode\WS;
4
5
use Wead\ZipCode\Contracts\ProviderContract;
6
7
class ApiCEP extends ProviderContract
8
{
9
    public function getAddressFromZipcode($zipCode)
10
    {
11
        $zipCode = preg_replace('/[^0-9]/im', '', $zipCode);
12
13
        $endPoint = "https://ws.apicep.com/cep/{$zipCode}.json";
14
15
        $content = file_get_contents($endPoint);
16
17
        $response = json_decode($content);
18
19
        return $this->normalizeResponse((array)$response);
20
    }
21
22
    private function normalizeResponse($address)
23
    {
24
        if (sizeof($address) > 0 && strlen($address['address']) > 0) {
25
            return [
26
                "status" => true,
27
                "address" => $address["address"],
28
                "district" => $address["district"],
29
                "city" => $address["city"],
30
                "state" => $address["state"],
31
                "api" => "ApiCEP"
32
            ];
33
        } else {
34
            return [
35
                "status" => false,
36
                "address" => null,
37
                "district" => null,
38
                "city" => null,
39
                "state" => null,
40
                "api" => "ApiCEP"
41
            ];
42
        }
43
    }
44
}