Complex classes like Ajuste 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 Ajuste, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Ajuste extends Configuracao implements Evento |
||
|
|
|||
| 37 | { |
||
| 38 | |||
| 39 | private $pasta_xml_base; |
||
| 40 | private $pasta_xml_inutilizado; |
||
| 41 | private $pasta_xml_cancelado; |
||
| 42 | private $pasta_xml_pendente; |
||
| 43 | private $pasta_xml_denegado; |
||
| 44 | private $pasta_xml_rejeitado; |
||
| 45 | private $pasta_xml_autorizado; |
||
| 46 | private $pasta_xml_processamento; |
||
| 47 | private $pasta_xml_assinado; |
||
| 48 | |||
| 49 | 7 | public function __construct($ajuste = array()) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Caminho da pasta base para armazenamento dos XML |
||
| 72 | */ |
||
| 73 | 1 | public function getPastaXmlBase() |
|
| 77 | |||
| 78 | 7 | public function setPastaXmlBase($pasta_xml_base) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Pasta onde ficam os XML das inutilizações de números de notas |
||
| 86 | */ |
||
| 87 | private function aplicaAmbiente($ambiente, $caminho) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Pasta onde ficam os XML das inutilizações de números de notas |
||
| 103 | */ |
||
| 104 | 1 | public function getPastaXmlInutilizado($ambiente = null) |
|
| 111 | |||
| 112 | 7 | public function setPastaXmlInutilizado($pasta_xml_inutilizado) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Pasta onde ficam os XML das notas após serem aceitas e depois canceladas |
||
| 120 | */ |
||
| 121 | 1 | public function getPastaXmlCancelado($ambiente = null) |
|
| 128 | |||
| 129 | 7 | public function setPastaXmlCancelado($pasta_xml_cancelado) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Pasta onde ficam os XML das notas pendentes de consulta |
||
| 137 | */ |
||
| 138 | 1 | public function getPastaXmlPendente($ambiente = null) |
|
| 145 | |||
| 146 | 7 | public function setPastaXmlPendente($pasta_xml_pendente) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Pasta onde ficam os XMLs após enviados e denegados |
||
| 154 | */ |
||
| 155 | 1 | public function getPastaXmlDenegado($ambiente = null) |
|
| 162 | |||
| 163 | 7 | public function setPastaXmlDenegado($pasta_xml_denegado) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Pasta onde ficam os XML das notas após serem enviadas e rejeitadas |
||
| 171 | */ |
||
| 172 | 1 | public function getPastaXmlRejeitado($ambiente = null) |
|
| 179 | |||
| 180 | 7 | public function setPastaXmlRejeitado($pasta_xml_rejeitado) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Pasta onde ficam os XML das notas após serem enviados e aceitos pela |
||
| 188 | * SEFAZ |
||
| 189 | */ |
||
| 190 | 1 | public function getPastaXmlAutorizado($ambiente = null) |
|
| 197 | |||
| 198 | 7 | public function setPastaXmlAutorizado($pasta_xml_autorizado) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Pasta onde ficam os XML das notas em processamento de retorno de |
||
| 206 | * autorização |
||
| 207 | */ |
||
| 208 | 1 | public function getPastaXmlProcessamento($ambiente = null) |
|
| 215 | |||
| 216 | 7 | public function setPastaXmlProcessamento($pasta_xml_processamento) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Pasta onde ficam os XMLs após assinado e antes de serem enviados |
||
| 224 | */ |
||
| 225 | 1 | public function getPastaXmlAssinado($ambiente = null) |
|
| 232 | |||
| 233 | 7 | public function setPastaXmlAssinado($pasta_xml_assinado) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Chamado quando o XML da nota foi gerado |
||
| 241 | */ |
||
| 242 | public function onNotaGerada($nota, $xml) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Chamado após o XML da nota ser assinado |
||
| 249 | */ |
||
| 250 | public function onNotaAssinada($nota, $xml) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Chamado após o XML da nota ser validado com sucesso |
||
| 257 | */ |
||
| 258 | public function onNotaValidada($nota, $xml) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Chamado antes de enviar a nota para a SEFAZ |
||
| 273 | */ |
||
| 274 | public function onNotaEnviando($nota, $xml) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Chamado quando a forma de emissão da nota fiscal muda para contigência, |
||
| 281 | * aqui deve ser decidido se o número da nota deverá ser pulado e se esse |
||
| 282 | * número deve ser cancelado ou inutilizado |
||
| 283 | */ |
||
| 284 | public function onNotaContingencia($nota, $offline, $exception) |
||
| 285 | { |
||
| 286 | echo 'Forma de emissão alterada para "'.$nota->getEmissao().'" <br>'; |
||
| 287 | // remove o XML salvo anteriormente com a emissão normal |
||
| 288 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 289 | if (file_exists($filename) && $offline) { |
||
| 290 | unlink($filename); // só remove se tiver certeza que nenhum dado foi enviado para a SEFAZ |
||
| 291 | } |
||
| 292 | // incrementa o número da nota se existir a possibilidade de ter enviado com sucesso |
||
| 293 | if (!$offline) { |
||
| 294 | $nota->setNumero($nota->getNumero() + 1); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Chamado quando a nota foi enviada e aceita pela SEFAZ |
||
| 300 | */ |
||
| 301 | 1 | public function onNotaAutorizada($nota, $xml, $retorno) |
|
| 302 | 1 | { |
|
| 303 | //echo 'XML autorizado com sucesso!<br>'; |
||
| 304 | |||
| 305 | // TODO: obter o estado da nota e remover apenas do local correto |
||
| 306 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 307 | if (file_exists($filename)) { |
||
| 308 | unlink($filename); |
||
| 309 | } |
||
| 310 | |||
| 311 | $filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 312 | if (file_exists($filename)) { |
||
| 313 | unlink($filename); |
||
| 314 | } |
||
| 315 | |||
| 316 | $filename = $this->getPastaXmlAutorizado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 317 | file_put_contents($filename, $xml->saveXML()); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Chamado quando a emissão da nota foi concluída com sucesso independente |
||
| 322 | * da forma de emissão |
||
| 323 | */ |
||
| 324 | public function onNotaCompleto($nota, $xml) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Chamado quando uma nota é rejeitada pela SEFAZ, a nota deve ser |
||
| 331 | * corrigida para depois ser enviada novamente |
||
| 332 | */ |
||
| 333 | public function onNotaRejeitada($nota, $xml, $retorno) |
||
| 334 | { |
||
| 335 | //echo 'XML rejeitado!<br>'; |
||
| 336 | |||
| 337 | // TODO: obter o estado da nota e remover apenas do local correto |
||
| 338 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 339 | if (file_exists($filename)) { |
||
| 340 | unlink($filename); |
||
| 341 | } |
||
| 342 | |||
| 343 | $filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 344 | if (file_exists($filename)) { |
||
| 345 | unlink($filename); |
||
| 346 | } |
||
| 347 | |||
| 348 | $filename = $this->getPastaXmlRejeitado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 349 | file_put_contents($filename, $xml->saveXML()); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Chamado quando a nota é denegada e não pode ser utilizada (outra nota |
||
| 354 | * deve ser gerada) |
||
| 355 | */ |
||
| 356 | public function onNotaDenegada($nota, $xml, $retorno) |
||
| 357 | { |
||
| 358 | //echo 'XML denagado!<br>'; |
||
| 359 | |||
| 360 | // TODO: obter o estado da nota e remover apenas do local correto |
||
| 361 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 362 | if (file_exists($filename)) { |
||
| 363 | unlink($filename); |
||
| 364 | } |
||
| 365 | |||
| 366 | $filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 367 | if (file_exists($filename)) { |
||
| 368 | unlink($filename); |
||
| 369 | } |
||
| 370 | |||
| 371 | $filename = $this->getPastaXmlDenegado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 372 | file_put_contents($filename, $xml->saveXML()); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Chamado após tentar enviar uma nota e não ter certeza se ela foi |
||
| 377 | * recebida ou não (problemas técnicos), deverá ser feito uma consulta pela |
||
| 378 | * chave para obter o estado da nota |
||
| 379 | */ |
||
| 380 | public function onNotaPendente($nota, $xml, $exception) |
||
| 381 | { |
||
| 382 | //echo 'XML pendente!<br>'; |
||
| 383 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 384 | if (file_exists($filename)) { |
||
| 385 | unlink($filename); |
||
| 386 | } |
||
| 387 | |||
| 388 | $filename = $this->getPastaXmlPendente($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 389 | file_put_contents($filename, $xml->saveXML()); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Chamado quando uma nota é enviada, mas não retornou o protocolo que será |
||
| 394 | * consultado mais tarde |
||
| 395 | */ |
||
| 396 | public function onNotaProcessando($nota, $xml, $retorno) |
||
| 397 | { |
||
| 398 | //echo 'XML em processamento!<br>'; |
||
| 399 | $filename = $this->getPastaXmlAssinado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 400 | if (file_exists($filename)) { |
||
| 401 | unlink($filename); |
||
| 402 | } |
||
| 403 | |||
| 404 | $filename = $this->getPastaXmlProcessamento($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 405 | file_put_contents($filename, $xml->saveXML()); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Chamado quando uma nota autorizada é cancelada na SEFAZ |
||
| 410 | */ |
||
| 411 | public function onNotaCancelada($nota, $xml, $retorno) |
||
| 412 | { |
||
| 413 | //echo 'XML cancelado!<br>'; |
||
| 414 | $filename = $this->getPastaXmlAutorizado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 415 | if (file_exists($filename)) { |
||
| 416 | unlink($filename); |
||
| 417 | } |
||
| 418 | |||
| 419 | $filename = $this->getPastaXmlCancelado($nota->getAmbiente()) . '/' . $nota->getID() . '.xml'; |
||
| 420 | file_put_contents($filename, $xml->saveXML()); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Chamado quando ocorre um erro nas etapas de geração e envio da nota (Não |
||
| 425 | * é chamado quando entra em contigência) |
||
| 426 | */ |
||
| 427 | public function onNotaErro($nota, $exception) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Chamado quando um ou mais números de notas forem inutilizados |
||
| 434 | */ |
||
| 435 | public function onInutilizado($inutilizacao, $xml) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Chamado quando uma ação é executada |
||
| 443 | */ |
||
| 444 | public function onTarefaExecutada($tarefa, $retorno) |
||
| 445 | { |
||
| 446 | //echo 'Tarefa executada!<br>'; |
||
| 447 | $nota = $tarefa->getNota(); |
||
| 448 | $xml = $tarefa->getDocumento(); |
||
| 449 | if ($tarefa->getAcao() == Tarefa::ACAO_CANCELAR && $retorno->isCancelado()) { |
||
| 450 | $filename = $this->getPastaXmlCancelado($nota->getAmbiente()) . '/' . $nota->getID() . '-procEventoNFe.xml'; |
||
| 451 | file_put_contents($filename, $xml->saveXML()); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Chamado quando ocorre uma falha na execução de uma tarefa |
||
| 457 | */ |
||
| 458 | public function onTarefaErro($tarefa, $exception) |
||
| 462 | |||
| 463 | 1 | public function toArray() |
|
| 477 | |||
| 478 | 7 | public function fromArray($ajuste = array()) |
|
| 479 | { |
||
| 480 | 7 | if ($ajuste instanceof Ajuste) { |
|
| 481 | 1 | $ajuste = $ajuste->toArray(); |
|
| 482 | 7 | } elseif (!is_array($ajuste)) { |
|
| 483 | 1 | return $this; |
|
| 533 | } |
||
| 534 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.