Cep   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddr() 0 11 2
A __construct() 0 3 1
A getError() 0 3 1
1
<?php
2
3
namespace ThallesDKoester\Entregas;
4
5
use Exception;
6
7
/**
8
 * Thalles D. Koester | Class Cep
9
 *
10
 * @author  Thalles D. koester <[email protected]>
11
 * @package ThallesDKoester\Entregas
12
 */
13
class Cep
14
{
15
    use Curl;
16
17
    const URL_VIACEP = 'http://viacep.com.br/ws';
18
19
    /** @var string */
20
    private $cep;
21
22
    /** @var string */
23
    private $error;
24
25
    /**
26
     * Cep constructor.
27
     * @param string $cep
28
     */
29
    public function __construct(string $cep)
30
    {
31
        $this->cep = preg_replace("/[^0-9]/", "", $cep);
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getError(): string
38
    {
39
        return $this->error;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getAddr(): ?array
46
    {
47
        $url = self::URL_VIACEP . "/{$this->cep}/json/";
48
49
        try {
50
            $data = $this->request($url, 'GET');
51
        } catch (Exception $e) {
52
            $this->error = $e;
53
            $data = null;
54
        } finally {
55
            return array_filter(json_decode($data, true));
56
        }
57
    }
58
}