Complex classes like Tarefa 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 Tarefa, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Tarefa |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Ação a ser realizada sobre o objeto ou recibo |
||
| 35 | */ |
||
| 36 | const ACAO_CONSULTAR = 'consultar'; |
||
| 37 | const ACAO_INUTILIZAR = 'inutilizar'; |
||
| 38 | const ACAO_CANCELAR = 'cancelar'; |
||
| 39 | |||
| 40 | private $id; |
||
| 41 | private $acao; |
||
| 42 | private $nota; |
||
| 43 | private $documento; |
||
| 44 | private $agente; |
||
| 45 | private $resposta; |
||
| 46 | |||
| 47 | 17 | public function __construct($tarefa = []) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Código aleatório e opcional que identifica a tarefa |
||
| 54 | */ |
||
| 55 | 16 | public function getID() |
|
| 59 | |||
| 60 | 17 | public function setID($id) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Ação a ser realizada sobre o objeto ou recibo |
||
| 68 | */ |
||
| 69 | 16 | public function getAcao() |
|
| 73 | |||
| 74 | 17 | public function setAcao($acao) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Nota que será processada se informado |
||
| 82 | */ |
||
| 83 | 16 | public function getNota() |
|
| 87 | |||
| 88 | 17 | public function setNota($nota) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Informa o XML do objeto, quando não informado o XML é gerado a partir do |
||
| 96 | * objeto |
||
| 97 | */ |
||
| 98 | 7 | public function getDocumento() |
|
| 102 | |||
| 103 | 17 | public function setDocumento($documento) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Agente que obteve ou vai obter a resposta, podendo ser: pedido de |
||
| 111 | * inutilização(NF\Inutilizacao), recibo(NF\Recibo) ou pedido de |
||
| 112 | * cancelamento(NF\Evento) |
||
| 113 | */ |
||
| 114 | 16 | public function getAgente() |
|
| 118 | |||
| 119 | 17 | public function setAgente($agente) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Resposta da tarefa após ser executada |
||
| 127 | */ |
||
| 128 | 7 | public function getResposta() |
|
| 132 | |||
| 133 | 17 | public function setResposta($resposta) |
|
| 138 | |||
| 139 | 3 | public function toArray($recursive = false) |
|
| 140 | { |
||
| 141 | 3 | $tarefa = []; |
|
| 142 | 3 | $tarefa['id'] = $this->getID(); |
|
| 143 | 3 | $tarefa['acao'] = $this->getAcao(); |
|
| 144 | 3 | if (!is_null($this->getNota()) && $recursive) { |
|
| 145 | 1 | $tarefa['nota'] = $this->getNota()->toArray($recursive); |
|
| 146 | } else { |
||
| 147 | 2 | $tarefa['nota'] = $this->getNota(); |
|
| 148 | } |
||
| 149 | 3 | if (!is_null($this->getDocumento()) && $recursive) { |
|
| 150 | 1 | $tarefa['documento'] = $this->getDocumento()->saveXML(); |
|
| 151 | } else { |
||
| 152 | 3 | $tarefa['documento'] = $this->getDocumento(); |
|
| 153 | } |
||
| 154 | 3 | if (!is_null($this->getAgente()) && $recursive) { |
|
| 155 | 1 | $tarefa['agente'] = $this->getAgente()->toArray($recursive); |
|
| 156 | } else { |
||
| 157 | 2 | $tarefa['agente'] = $this->getAgente(); |
|
| 158 | } |
||
| 159 | 3 | if (!is_null($this->getResposta()) && $recursive) { |
|
| 160 | 1 | $tarefa['resposta'] = $this->getResposta()->toArray($recursive); |
|
| 161 | } else { |
||
| 162 | 2 | $tarefa['resposta'] = $this->getResposta(); |
|
| 163 | } |
||
| 164 | 3 | return $tarefa; |
|
| 165 | } |
||
| 166 | |||
| 167 | 17 | public function fromArray($tarefa = []) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Resposta da tarefa após ser executada |
||
| 209 | */ |
||
| 210 | 15 | public function executa() |
|
| 230 | |||
| 231 | 4 | private function cancela() |
|
| 270 | |||
| 271 | 6 | private function inutiliza() |
|
| 272 | { |
||
| 273 | 6 | $nota = $this->getNota(); |
|
| 274 | 6 | $inutilizacao = $this->getAgente(); |
|
| 275 | 6 | if (is_null($inutilizacao)) { |
|
| 276 | 2 | if (is_null($nota)) { |
|
| 277 | 1 | throw new \Exception('A nota não foi informada na tarefa de inutilização "'.$this->getID().'"', 404); |
|
| 278 | } |
||
| 279 | 1 | $inutilizacao = new Inutilizacao(); |
|
| 280 | 1 | $inutilizacao->setAno(date('Y')); |
|
| 281 | 1 | $inutilizacao->setJustificativa($nota->getJustificativa()); |
|
| 282 | 1 | $this->setAgente($inutilizacao); |
|
| 283 | 4 | } elseif (!($inutilizacao instanceof Inutilizacao)) { |
|
| 284 | 1 | throw new \Exception('O agente informado não é uma inutilização', 500); |
|
| 285 | } |
||
| 286 | 4 | if (!is_null($nota)) { |
|
| 287 | 2 | $inutilizacao->setCNPJ($nota->getEmitente()->getCNPJ()); |
|
| 288 | 2 | $inutilizacao->setSerie($nota->getSerie()); |
|
| 289 | 2 | $inutilizacao->setInicio($nota->getNumero()); |
|
| 290 | 2 | $inutilizacao->setFinal($nota->getNumero()); |
|
| 291 | 2 | $inutilizacao->setUF($nota->getEmitente()->getEndereco()-> |
|
| 292 | 2 | getMunicipio()->getEstado()->getUF()); |
|
| 293 | 2 | $inutilizacao->setAmbiente($nota->getAmbiente()); |
|
| 294 | 2 | $inutilizacao->setModelo($nota->getModelo()); |
|
| 295 | } |
||
| 296 | 4 | $dom = $inutilizacao->getNode()->ownerDocument; |
|
| 297 | 3 | $dom = $inutilizacao->assinar($dom); |
|
| 298 | 3 | $dom = $inutilizacao->envia($dom); |
|
| 299 | 2 | $this->setDocumento($dom); |
|
| 300 | 2 | return $inutilizacao; |
|
| 301 | } |
||
| 302 | |||
| 303 | 5 | private function consulta() |
|
| 334 | } |
||
| 335 |