ApiCEP::normalizeResponse()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 2
nop 1
dl 0
loc 19
rs 9.7333
c 1
b 0
f 0
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
        $zipCode = substr($zipCode, 0, 5) . "-" . substr($zipCode, 5);
14
15
        $endPoint = "https://cdn.apicep.com/file/apicep/{$zipCode}.json";
16
17
        $content = @file_get_contents($endPoint);
18
19
        $response = json_decode($content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
        $response = json_decode(/** @scrutinizer ignore-type */ $content);
Loading history...
20
21
        return $this->normalizeResponse((array)$response);
22
    }
23
24
    private function normalizeResponse($address)
25
    {
26
        if (sizeof($address) > 0 && strlen($address['address']) > 0) {
27
            return [
28
                "status" => true,
29
                "address" => $address["address"],
30
                "district" => $address["district"],
31
                "city" => $address["city"],
32
                "state" => $address["state"],
33
                "api" => "ApiCEP"
34
            ];
35
        } else {
36
            return [
37
                "status" => false,
38
                "address" => null,
39
                "district" => null,
40
                "city" => null,
41
                "state" => null,
42
                "api" => "ApiCEP"
43
            ];
44
        }
45
    }
46
}
47