Complex classes like SEFAZ often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SEFAZ, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class SEFAZ |
||
39 | { |
||
40 | |||
41 | private $notas; |
||
42 | private $configuracao; |
||
43 | private static $instance; |
||
44 | |||
45 | 100 | public function __construct($sefaz = []) |
|
49 | |||
50 | 104 | public static function getInstance($new = false) |
|
51 | { |
||
52 | 104 | if (is_null(self::$instance) || $new) { |
|
53 | 99 | self::$instance = new self(); |
|
54 | } |
||
55 | 104 | return self::$instance; |
|
56 | } |
||
57 | |||
58 | 4 | public function getNotas() |
|
62 | |||
63 | 100 | public function setNotas($notas) |
|
68 | |||
69 | 3 | public function addNota($nota) |
|
74 | |||
75 | 94 | public function getConfiguracao() |
|
79 | |||
80 | 100 | public function setConfiguracao($configuracao) |
|
85 | |||
86 | 1 | public function toArray($recursive = false) |
|
106 | |||
107 | 100 | public function fromArray($sefaz = []) |
|
122 | |||
123 | 1 | private function despacha($nota, &$dom, $retorno) |
|
124 | { |
||
125 | 1 | $evento = $this->getConfiguracao()->getEvento(); |
|
126 | 1 | if ($retorno->isRecebido()) { |
|
127 | Log::debug('SEFAZ.despacha - Recibo: '.$retorno->getNumero().' da '.$nota->getID(true)); |
||
128 | $evento->onNotaProcessando($nota, $dom, $retorno); |
||
129 | 1 | } elseif ($retorno->isAutorizado()) { |
|
130 | 1 | $dom = $nota->addProtocolo($dom); |
|
131 | 1 | Log::debug('SEFAZ.despacha('.$retorno->getStatus().') - '.$retorno->getMotivo(). |
|
132 | 1 | ', Protocolo: '.$retorno->getNumero().' - '.$nota->getID(true)); |
|
133 | 1 | $evento->onNotaAutorizada($nota, $dom, $retorno); |
|
134 | } elseif ($retorno->isDenegada()) { |
||
135 | $evento->onNotaDenegada($nota, $dom, $retorno); |
||
136 | } elseif ($retorno->isCancelado()) { |
||
137 | $evento->onNotaCancelada($nota, $dom, $retorno); |
||
138 | } else { |
||
139 | $evento->onNotaRejeitada($nota, $dom, $retorno); |
||
140 | throw new \Exception($retorno->getMotivo(), $retorno->getStatus()); |
||
141 | } |
||
142 | 1 | } |
|
143 | |||
144 | /** |
||
145 | * Envia as notas adicionadas para a SEFAZ, caso não consiga, torna-as em contingência |
||
146 | * todos os status são informados no evento da configuração, caso não possua evento, |
||
147 | * uma \Exception será lançada na primeira falha |
||
148 | */ |
||
149 | 3 | public function autoriza() |
|
150 | { |
||
151 | 3 | $i = 0; |
|
152 | 3 | $evento = $this->getConfiguracao()->getEvento(); |
|
153 | 3 | foreach ($this->getNotas() as $nota) { |
|
154 | try { |
||
155 | 2 | $envia = true; |
|
156 | do { |
||
157 | 2 | $dom = $nota->getNode()->ownerDocument; |
|
158 | 2 | $evento->onNotaGerada($nota, $dom); |
|
159 | 2 | $dom = $nota->assinar($dom); |
|
160 | 2 | $evento->onNotaAssinada($nota, $dom); |
|
161 | 2 | $dom = $nota->validar($dom); // valida o XML da nota |
|
162 | 2 | $evento->onNotaValidada($nota, $dom); |
|
163 | 2 | if (!$envia) { |
|
164 | 1 | break; |
|
165 | } |
||
166 | 2 | $evento->onNotaEnviando($nota, $dom); |
|
167 | 2 | $autorizacao = new Autorizacao(); |
|
168 | try { |
||
169 | 2 | $retorno = $autorizacao->envia($nota, $dom); |
|
170 | 1 | } catch (\Exception $e) { |
|
171 | 1 | $partial_response = $e instanceof \NFe\Exception\IncompleteRequestException; |
|
172 | 1 | if ($partial_response) { |
|
173 | 1 | $evento->onNotaPendente($nota, $dom, $e); |
|
174 | } |
||
175 | 1 | if ($nota->getEmissao() == Nota::EMISSAO_CONTINGENCIA) { |
|
176 | throw $e; |
||
177 | } |
||
178 | 1 | Log::debug('SEFAZ.autoriza('.$e->getCode().') - Mudando emissão para contingência: '. |
|
179 | 1 | $e->getMessage().' - '.$nota->getID(true)); |
|
180 | 1 | $msg = substr('Falha no envio da nota: '.$e->getMessage(), 0, 256); |
|
181 | 1 | $nota->setEmissao(Nota::EMISSAO_CONTINGENCIA); |
|
182 | 1 | $nota->setDataContingencia(time()); |
|
183 | 1 | $nota->setJustificativa($msg); |
|
184 | 1 | $evento->onNotaContingencia($nota, !$partial_response, $e); |
|
185 | 1 | $envia = false; |
|
186 | 1 | continue; |
|
187 | } |
||
188 | 1 | Log::debug('SEFAZ.autoriza('.$retorno->getStatus().') - '. |
|
189 | 1 | $retorno->getMotivo().' - '.$nota->getID(true)); |
|
190 | 1 | $this->despacha($nota, $dom, $retorno); |
|
191 | 1 | break; |
|
192 | 1 | } while (true); |
|
193 | 2 | $evento->onNotaCompleto($nota, $dom); |
|
194 | 2 | $i++; |
|
195 | } catch (\Exception $e) { |
||
196 | Log::error('SEFAZ.autoriza('.$e->getCode().') - '.$e->getMessage()); |
||
197 | 2 | $evento->onNotaErro($nota, $e); |
|
198 | } |
||
199 | } |
||
200 | 3 | return $i; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Consulta o status das notas em processamento |
||
205 | */ |
||
206 | 2 | public function consulta($pendencias) |
|
229 | |||
230 | /* Consulta se as notas existem e cancela ou inutiliza seus números |
||
231 | * Também processa pedido de inutilização e cancelamento de notas |
||
232 | */ |
||
233 | 4 | public function executa($tarefas) |
|
265 | |||
266 | /* * |
||
267 | * Inutiliza um intervalo de números de notas fiscais e insere o resultado no |
||
268 | * próprio objeto de inutilização |
||
269 | */ |
||
270 | 2 | public function inutiliza($inutilizacao) |
|
283 | |||
284 | /** |
||
285 | * Obtém as notas pendentes de autorização e envia para a SEFAZ |
||
286 | */ |
||
287 | 1 | public function processa() |
|
319 | } |
||
320 |
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.