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

Search::getAddressFromZipcode()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 22
c 3
b 0
f 0
nc 24
nop 1
dl 0
loc 37
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Classe principal para as buscas de CEP
4
 *
5
 * Recebe o cep a ser procurado e randomicamente consulta os serviços
6
 * online, retorna o resultado do primeiro que encontrar
7
 *
8
 * @author  Adriano Maciel <[email protected]>
9
 * @license https://opensource.org/licenses/MIT MIT Licence
10
 */
11
12
namespace Wead\ZipCode;
13
14
use Wead\ZipCode\Exceptions\ZipCodeNotFoundException;
15
use Wead\ZipCode\WS\CepLa;
16
use Wead\ZipCode\WS\ViaCep;
17
use Wead\ZipCode\WS\WideNet;
18
use Wead\ZipCode\WS\WebMania;
19
20
class Search
21
{
22
    private $listApi = [
23
        'ViaCep',
24
        'WebMania',
25
        'WideNet',
26
        'CepLa'
27
    ];
28
29
    private $credential = [
30
        'webMania' => [
31
            'apiKey' => null,
32
            'apiSecret' => null
33
        ]
34
    ];
35
36
    public function setCredential($service, $credential = [])
37
    {
38
        if (is_string($service) && is_array($credential)) {
39
            $this->credential[$service] = $credential;
40
        }
41
    }
42
43
    public function getAddressFromZipcode($zipCode)
44
    {
45
        $found = false;
46
        
47
        shuffle($this->listApi);
48
49
        foreach ($this->listApi as $api) {
50
            if (!$found) {
51
                switch ($api) {
52
                    case 'ViaCep':
53
                        $found = $this->getFromViaCep($zipCode);
54
                        break;
55
56
                    case 'WebMania':
57
                        $found = $this->getFromWebMania($zipCode);
58
                        break;
59
60
                    case 'WideNet':
61
                        $found = $this->getFromWideNet($zipCode);
62
                        break;
63
64
                    case 'CepLa':
65
                        $found = $this->getFromCepLa($zipCode);
66
                        break;
67
                }
68
                
69
                if (!isset($found['address']) || !$found['status']) {
70
                    $found = false;
71
                }
72
            }
73
        }
74
75
        if (!$found) {
76
            throw new ZipCodeNotFoundException("Error to get address by zipcode: {$zipCode}");
77
        }
78
79
        return $found;
80
    }
81
82
    private function getFromViaCep($zipCode)
83
    {
84
        $zip = new ViaCep(isset($this->credential['viaCep']) ? $this->credential['viaCep'] : []);
85
        $zip = $zip->getAddressFromZipcode($zipCode);
86
87
        return $zip;
88
    }
89
90
    private function getFromWebMania($zipCode)
91
    {
92
        $zip = new WebMania(isset($this->credential['webMania']) ? $this->credential['webMania'] : []);
93
        $zip = $zip->getAddressFromZipcode($zipCode);
94
95
        return $zip;
96
    }
97
98
    private function getFromWideNet($zipCode)
99
    {
100
        $zip = new WideNet(isset($this->credential['wideNet']) ? $this->credential['wideNet'] : []);
101
        $zip = $zip->getAddressFromZipcode($zipCode);
102
103
        return $zip;
104
    }
105
106
    private function getFromCepLa($zipCode)
107
    {
108
        $zip = new CepLa(isset($this->credential['cepLa']) ? $this->credential['cepLa'] : []);
109
        $zip = $zip->getAddressFromZipcode($zipCode);
110
111
        return $zip;
112
    }
113
}
114