Completed
Push — master ( dc5f67...84520a )
by Francimar
11:49 queued 07:55
created

IBPT   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 74.03%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 115
ccs 57
cts 77
cp 0.7403
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isOffline() 0 4 1
A setOffline() 0 4 1
A load() 0 17 4
A getImposto() 0 13 4
B getImpostoOnline() 0 44 3
A getImpostoOffline() 0 15 3
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Database;
29
30
use Curl\Curl;
31
use NFe\Log\Logger;
32
33
class IBPT
34
{
35
36
    private $tabela;
37
    private $offline;
38
39 89
    public function __construct()
40
    {
41 89
        $this->tabela = array();
42 89
        $this->offline = false;
43 89
    }
44
45 1
    public function isOffline()
46
    {
47 1
        return $this->offline;
48
    }
49
50 1
    public function setOffline($offline)
51
    {
52 1
        $this->offline = $offline;
53 1
    }
54
55 32
    private function load($uf)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $uf. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
56
    {
57 32
        if (isset($this->tabela[$uf])) {
58 28
            return $this->tabela[$uf];
59
        }
60 32
        $file = __DIR__ . '/data/IBPT/'.$uf.'.json';
61 32
        if (!file_exists($file)) {
62 2
            return false;
63
        }
64 32
        $content = file_get_contents($file);
65 32
        if ($content === false) {
66
            return false;
67
        }
68 32
        $data = json_decode($content, true);
69 32
        $this->tabela[$uf] = $data;
70 32
        return $data;
71
    }
72
73 32
    private function getImpostoOffline($ncm, $uf, $ex)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $uf. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
74
    {
75 32
        $data = $this->load($uf);
76 32
        if ($data === false) {
77 2
            return false;
78
        }
79 32
        $key = $ncm.'.'.sprintf('%02s', $ex);
80 32
        if (!isset($data['estados'][$uf][$key])) {
81 1
            return false;
82
        }
83 31
        $o = $data['estados'][$uf][$key];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
84 31
        $o['info'] = $data['info'];
85 31
        $o['info']['origem'] = 'Tabela offline';
86 31
        return $o;
87
    }
88
89 1
    private function getImpostoOnline($cnpj, $token, $ncm, $uf, $ex)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $uf. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
90
    {
91 1
        if ($this->isOffline()) {
92 1
            return false;
93
        }
94 1
        $url = 'http://iws.ibpt.org.br/api/Produtos';
95
        $params = array(
96 1
            'token' => $token,
97 1
            'cnpj' => $cnpj,
98 1
            'codigo' => $ncm,
99 1
            'uf' => $uf,
100 1
            'ex' => intval($ex)
101 1
        );
102 1
        $curl = new Curl($url);
103 1
        $curl->setConnectTimeout(2);
104 1
        $curl->setTimeout(3);
105 1
        $data = $curl->get($params);
106 1
        if ($curl->error) {
107 1
            Logger::warning('IBPT.getImpostoOnline('.$curl->errorCode.') - '.$curl->errorMessage);
0 ignored issues
show
Bug introduced by
The method warning() does not exist on NFe\Log\Logger. Did you maybe mean warningLog()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
108 1
            $this->setOffline(true);
109 1
            return false;
110
        }
111
        $o = array(
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
112
            'importado' => $data->Importado,
113
            'nacional' => $data->Nacional,
114
            'estadual' => $data->Estadual,
115
            'municipal' => $data->Municipal,
116
            'tipo' => $data->Tipo
117
        );
118
        $vigenciainicio = date_create_from_format('d/m/Y', $data->VigenciaInicio);
119
        $vigenciafim = date_create_from_format('d/m/Y', $data->VigenciaFim);
120
        $info = array(
121
            'origem' => 'API IBPT',
122
            'fonte' => $data->Fonte,
123
            'versao' => $data->Versao,
124
            'chave' => $data->Chave,
125
            'vigencia' => array(
126
                'inicio' => date_format($vigenciainicio, 'Y-m-d'),
127
                'fim' => date_format($vigenciafim, 'Y-m-d')
128
            )
129
        );
130
        $o['info'] = $info;
131
        return $o;
132
    }
133
134 32
    public function getImposto($cnpj, $token, $ncm, $uf, $ex)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $uf. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
135
    {
136 32
        $uf = strtoupper($uf);
137 32
        $uf = preg_replace('/[^A-Z]/', '', $uf);
138 32
        if (is_null($cnpj) || is_null($token)) {
139 32
            return $this->getImpostoOffline($ncm, $uf, $ex);
140
        }
141 1
        $o = $this->getImpostoOnline($cnpj, $token, $ncm, $uf, $ex);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
142 1
        if ($o === false) {
143 1
            return $this->getImpostoOffline($ncm, $uf, $ex);
144
        }
145
        return $o;
146
    }
147
}
148