Passed
Pull Request — master (#2)
by
unknown
04:30
created

Search   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 87.93%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 63
c 7
b 0
f 0
dl 0
loc 126
ccs 51
cts 58
cp 0.8793
rs 10
wmc 27

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCredential() 0 4 3
A setMaxAttempts() 0 3 1
C attemptSearch() 0 37 12
A getFromCepLa() 0 6 1
A getFromWideNet() 0 6 1
A getFromViaCep() 0 6 1
A getAddressFromZipcode() 0 22 6
A getFromWebMania() 0 6 2
1
<?php
2
3
/**
4
 * Classe principal para as buscas de CEP
5
 *
6
 * Recebe o cep a ser procurado e randomicamente consulta os serviços
7
 * online, retorna o resultado do primeiro que encontrar
8
 *
9
 * @author  Adriano Maciel <[email protected]>
10
 * @license https://opensource.org/licenses/MIT MIT Licence
11
 */
12
13
namespace Wead\ZipCode;
14
15
use Wead\ZipCode\Exceptions\ZipCodeNotFoundException;
16
use Wead\ZipCode\WS\CepLa;
17
use Wead\ZipCode\WS\ViaCep;
18
use Wead\ZipCode\WS\WideNet;
19
use Wead\ZipCode\WS\WebMania;
20
21
class Search
22
{
23
    private $listApi = [
24
        'ViaCep',
25
        'WebMania',
26
        'WideNet',
27
        'CepLa'
28
    ];
29
30
    private $credential = [
31
        'webMania' => [
32
            'apiKey' => null,
33
            'apiSecret' => null
34
        ]
35
    ];
36
37
    private $maxAttempts = 5;
38
    private $countAttempts = 0;
39
40 2
    public function setMaxAttempts($maxAttempts = 5)
41
    {
42 2
        return $this->maxAttempts = (int) $maxAttempts;
43
    }
44
45 2
    public function setCredential($service, $credential = [])
46
    {
47 2
        if (is_string($service) && is_array($credential)) {
48 2
            $this->credential[$service] = $credential;
49
        }
50 2
    }
51
52 1
    public function getAddressFromZipcode($zipCode)
53
    {
54
        try {
55 1
            $found = $this->attemptSearch($zipCode);
56
57 1
            if (!$found['city']) {
58
                $this->countAttempts++;
59
60
                return $this->getAddressFromZipcode($zipCode);
61
            }
62
63 1
            $found['address'] = strlen(trim($found['address'])) == 0 ? "Não encontrado" : $found['address'];
64 1
            $found['district'] = strlen(trim($found['district'])) == 0 ? "Não encontrado" : $found['district'];
65
66 1
            return $found;
67
        } catch (\Exception $e) {
68
            if ($this->countAttempts < $this->maxAttempts) {
69
                $this->countAttempts++;
70
71
                return $this->getAddressFromZipcode($zipCode);
72
            } else {
73
                throw new \Exception("Not Found data for this cep");
74
            }
75
        }
76
    }
77
78 3
    private function attemptSearch($zipCode)
79
    {
80 3
        $found = false;
81
82 3
        shuffle($this->listApi);
83
84 3
        foreach ($this->listApi as $api) {
85 3
            if (!$found) {
86 3
                switch ($api) {
87 3
                    case 'ViaCep':
88 2
                        $found = $this->getFromViaCep($zipCode);
89 2
                        break;
90
91 2
                    case 'WebMania':
92 1
                        $found = $this->getFromWebMania($zipCode);
93 1
                        break;
94
95 2
                    case 'WideNet':
96 1
                        $found = $this->getFromWideNet($zipCode);
97 1
                        break;
98
99 2
                    case 'CepLa':
100 2
                        $found = $this->getFromCepLa($zipCode);
101 2
                        break;
102
                }
103
104 3
                if (!isset($found['city']) || !$found['status'] || !$found['city'] || strlen(trim($found['city'])) == 0) {
105 3
                    $found = false;
106
                }
107
            }
108
        }
109
110 3
        if (!$found) {
111 1
            throw new ZipCodeNotFoundException("Error to get address by zipcode: {$zipCode}");
112
        }
113
114 2
        return $found;
115
    }
116
117 3
    private function getFromViaCep($zipCode)
118
    {
119 3
        $zip = new ViaCep();
120 3
        $zip = $zip->getAddressFromZipcode($zipCode);
121
122 3
        return $zip;
123
    }
124
125 2
    private function getFromWebMania($zipCode, $runningTest = false)
126
    {
127 2
        $zip = new WebMania(isset($this->credential['webMania']) ? $this->credential['webMania'] : []);
128 2
        $zip = $zip->getAddressFromZipcode($zipCode, $runningTest);
129
130 2
        return $zip;
131
    }
132
133 2
    private function getFromWideNet($zipCode)
134
    {
135 2
        $zip = new WideNet();
136 2
        $zip = $zip->getAddressFromZipcode($zipCode);
137
138 2
        return $zip;
139
    }
140
141 3
    private function getFromCepLa($zipCode)
142
    {
143 3
        $zip = new CepLa();
144 3
        $zip = $zip->getAddressFromZipcode($zipCode);
145
146 3
        return $zip;
147
    }
148
}
149