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 | 101 | public function __construct($sefaz = []) |
|
| 49 | |||
| 50 | 105 | public static function getInstance($new = false) |
|
| 51 | { |
||
| 52 | 105 | if (is_null(self::$instance) || $new) { |
|
| 53 | 100 | self::$instance = new self(); |
|
| 54 | } |
||
| 55 | 105 | return self::$instance; |
|
| 56 | } |
||
| 57 | |||
| 58 | 4 | public function getNotas() |
|
| 62 | |||
| 63 | 101 | public function setNotas($notas) |
|
| 68 | |||
| 69 | 3 | public function addNota($nota) |
|
| 74 | |||
| 75 | 95 | public function getConfiguracao() |
|
| 79 | |||
| 80 | 101 | public function setConfiguracao($configuracao) |
|
| 85 | |||
| 86 | 1 | public function toArray($recursive = false) |
|
| 106 | |||
| 107 | 101 | public function fromArray($sefaz = []) |
|
| 122 | |||
| 123 | private function despacha($nota, &$dom, $retorno) |
||
| 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 | 2 | break; |
|
| 165 | } |
||
| 166 | 2 | $evento->onNotaEnviando($nota, $dom); |
|
| 167 | 2 | $autorizacao = new Autorizacao(); |
|
| 168 | try { |
||
| 169 | 2 | $retorno = $autorizacao->envia($nota, $dom); |
|
| 170 | 2 | } catch (\Exception $e) { |
|
| 171 | 2 | $partial_response = $e instanceof \NFe\Exception\IncompleteRequestException; |
|
| 172 | 2 | if ($partial_response) { |
|
| 173 | 1 | $evento->onNotaPendente($nota, $dom, $e); |
|
| 174 | } |
||
| 175 | 2 | if ($nota->getEmissao() == Nota::EMISSAO_CONTINGENCIA) { |
|
| 176 | throw $e; |
||
| 177 | } |
||
| 178 | 2 | Log::debug('SEFAZ.autoriza('.$e->getCode().') - Mudando emissão para contingência: '. |
|
| 179 | 2 | $e->getMessage().' - '.$nota->getID(true)); |
|
| 180 | 2 | $msg = substr('Falha no envio da nota: '.$e->getMessage(), 0, 256); |
|
| 181 | 2 | $nota->setEmissao(Nota::EMISSAO_CONTINGENCIA); |
|
| 182 | 2 | $nota->setDataContingencia(time()); |
|
| 183 | 2 | $nota->setJustificativa($msg); |
|
| 184 | 2 | $evento->onNotaContingencia($nota, !$partial_response, $e); |
|
| 185 | 2 | $envia = false; |
|
| 186 | 2 | continue; |
|
| 187 | } |
||
| 188 | Log::debug('SEFAZ.autoriza('.$retorno->getStatus().') - '. |
||
| 189 | $retorno->getMotivo().' - '.$nota->getID(true)); |
||
| 190 | $this->despacha($nota, $dom, $retorno); |
||
| 191 | break; |
||
| 192 | 2 | } 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.