Passed
Push — master ( c3e9b1...93d330 )
by Adriano
03:44
created

Search::attemptSearch()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 22
nc 24
nop 1
dl 0
loc 37
ccs 23
cts 23
cp 1
crap 12
rs 6.9666
c 0
b 0
f 0

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
/**
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['address']) {
58
                $this->countAttempts++;
59
60
                return $this->getAddressFromZipcode($zipCode);
61
            }
62
63 1
            return $found;
64
        } catch (\Exception $e) {
65
            if ($this->countAttempts < $this->maxAttempts) {
66
                $this->countAttempts++;
67
68
                return $this->getAddressFromZipcode($zipCode);
69
            }
70
        }
71
    }
72
73 3
    private function attemptSearch($zipCode)
74
    {
75 3
        $found = false;
76
77 3
        shuffle($this->listApi);
78
79 3
        foreach ($this->listApi as $api) {
80 3
            if (!$found) {
81 3
                switch ($api) {
82 3
                    case 'ViaCep':
83 1
                        $found = $this->getFromViaCep($zipCode);
84 1
                        break;
85
86 3
                    case 'WebMania':
87 1
                        $found = $this->getFromWebMania($zipCode);
88 1
                        break;
89
90 3
                    case 'WideNet':
91 3
                        $found = $this->getFromWideNet($zipCode);
92 3
                        break;
93
94 1
                    case 'CepLa':
95 1
                        $found = $this->getFromCepLa($zipCode);
96 1
                        break;
97
                }
98
99 3
                if (!isset($found['address']) || !$found['status'] || !$found['address'] || strlen(trim($found['address'])) == 0) {
100 3
                    $found = false;
101
                }
102
            }
103
        }
104
105 3
        if (!$found) {
106 1
            throw new ZipCodeNotFoundException("Error to get address by zipcode: {$zipCode}");
107
        }
108
109 2
        return $found;
110
    }
111
112 2
    private function getFromViaCep($zipCode)
113
    {
114 2
        $zip = new ViaCep();
115 2
        $zip = $zip->getAddressFromZipcode($zipCode);
116
117 2
        return $zip;
118
    }
119
120 2
    private function getFromWebMania($zipCode, $runningTest = false)
121
    {
122 2
        $zip = new WebMania(isset($this->credential['webMania']) ? $this->credential['webMania'] : []);
123 2
        $zip = $zip->getAddressFromZipcode($zipCode, $runningTest);
124
125 2
        return $zip;
126
    }
127
128 4
    private function getFromWideNet($zipCode)
129
    {
130 4
        $zip = new WideNet();
131 4
        $zip = $zip->getAddressFromZipcode($zipCode);
132
133 4
        return $zip;
134
    }
135
136 2
    private function getFromCepLa($zipCode)
137
    {
138 2
        $zip = new CepLa();
139 2
        $zip = $zip->getAddressFromZipcode($zipCode);
140
141 2
        return $zip;
142
    }
143
}
144