Complex classes like Recibo 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 Recibo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Recibo extends Retorno |
||
| 35 | { |
||
| 36 | |||
| 37 | const INFO_TAGNAME = 'infRec'; |
||
| 38 | |||
| 39 | private $numero; |
||
| 40 | private $tempo_medio; |
||
| 41 | private $codigo; |
||
| 42 | private $mensagem; |
||
| 43 | private $modelo; |
||
| 44 | |||
| 45 | 10 | public function __construct($recibo = array()) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Número do Recibo |
||
| 52 | */ |
||
| 53 | 5 | public function getNumero($normalize = false) |
|
| 54 | { |
||
| 55 | 5 | if (!$normalize) { |
|
| 56 | 1 | return $this->numero; |
|
| 57 | } |
||
| 58 | 5 | return $this->numero; |
|
| 59 | } |
||
| 60 | |||
| 61 | 10 | public function setNumero($numero) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Tempo médio de resposta do serviço (em segundos) dos últimos 5 minutos |
||
| 69 | */ |
||
| 70 | 1 | public function getTempoMedio($normalize = false) |
|
| 71 | { |
||
| 72 | 1 | if (!$normalize) { |
|
| 73 | 1 | return $this->tempo_medio; |
|
| 74 | } |
||
| 75 | return $this->tempo_medio; |
||
| 76 | } |
||
| 77 | |||
| 78 | 10 | public function setTempoMedio($tempo_medio) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Código da Mensagem (v2.0) alterado para tamanho variavel 1-4. |
||
| 86 | * (NT2011/004) |
||
| 87 | */ |
||
| 88 | 1 | public function getCodigo($normalize = false) |
|
| 89 | { |
||
| 90 | 1 | if (!$normalize) { |
|
| 91 | 1 | return $this->codigo; |
|
| 92 | } |
||
| 93 | return $this->codigo; |
||
| 94 | } |
||
| 95 | |||
| 96 | 10 | public function setCodigo($codigo) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Mensagem da SEFAZ para o emissor. (v2.0) |
||
| 104 | */ |
||
| 105 | 1 | public function getMensagem($normalize = false) |
|
| 106 | { |
||
| 107 | 1 | if (!$normalize) { |
|
| 108 | 1 | return $this->mensagem; |
|
| 109 | } |
||
| 110 | return $this->mensagem; |
||
| 111 | } |
||
| 112 | |||
| 113 | 10 | public function setMensagem($mensagem) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e. |
||
| 121 | * @param boolean $normalize informa se o modelo deve estar no formato do XML |
||
| 122 | * @return mixed modelo do Envio |
||
| 123 | */ |
||
| 124 | 4 | public function getModelo($normalize = false) |
|
| 125 | { |
||
| 126 | 4 | if (!$normalize) { |
|
| 127 | 4 | return $this->modelo; |
|
| 128 | } |
||
| 129 | switch ($this->modelo) { |
||
| 130 | case Nota::MODELO_NFE: |
||
| 131 | return '55'; |
||
| 132 | case Nota::MODELO_NFCE: |
||
| 133 | return '65'; |
||
| 134 | } |
||
| 135 | return $this->modelo; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Altera o valor do Modelo para o informado no parâmetro |
||
| 140 | * @param mixed $modelo novo valor para Modelo |
||
| 141 | * @return Envio A própria instância da classe |
||
| 142 | */ |
||
| 143 | 10 | public function setModelo($modelo) |
|
| 156 | |||
| 157 | 1 | public function toArray($recursive = false) |
|
| 158 | { |
||
| 159 | 1 | $recibo = parent::toArray($recursive); |
|
| 160 | 1 | $recibo['numero'] = $this->getNumero(); |
|
| 167 | |||
| 168 | 10 | public function fromArray($recibo = array()) |
|
| 169 | { |
||
| 170 | 10 | if ($recibo instanceof Recibo) { |
|
| 171 | 1 | $recibo = $recibo->toArray(); |
|
| 172 | 10 | } elseif (!is_array($recibo)) { |
|
| 173 | 1 | return $this; |
|
| 174 | } |
||
| 175 | 10 | parent::fromArray($recibo); |
|
| 176 | 10 | if (isset($recibo['numero'])) { |
|
| 177 | 1 | $this->setNumero($recibo['numero']); |
|
| 178 | } else { |
||
| 179 | 10 | $this->setNumero(null); |
|
| 180 | } |
||
| 181 | 10 | if (isset($recibo['tempo_medio'])) { |
|
| 182 | $this->setTempoMedio($recibo['tempo_medio']); |
||
| 183 | } else { |
||
| 184 | 10 | $this->setTempoMedio(null); |
|
| 185 | } |
||
| 186 | 10 | if (isset($recibo['codigo'])) { |
|
| 187 | $this->setCodigo($recibo['codigo']); |
||
| 188 | } else { |
||
| 189 | 10 | $this->setCodigo(null); |
|
| 190 | } |
||
| 191 | 10 | if (isset($recibo['mensagem'])) { |
|
| 192 | $this->setMensagem($recibo['mensagem']); |
||
| 193 | } else { |
||
| 194 | 10 | $this->setMensagem(null); |
|
| 195 | } |
||
| 196 | 10 | if (isset($recibo['modelo'])) { |
|
| 197 | 1 | $this->setModelo($recibo['modelo']); |
|
| 198 | } else { |
||
| 199 | 10 | $this->setModelo(null); |
|
| 200 | } |
||
| 201 | 10 | return $this; |
|
| 202 | } |
||
| 203 | |||
| 204 | 4 | public function envia($dom) |
|
| 221 | |||
| 222 | 5 | public function consulta($nota = null) |
|
| 223 | { |
||
| 224 | 5 | if (!is_null($nota)) { |
|
| 225 | 5 | $this->setAmbiente($nota->getAmbiente()); |
|
| 226 | 5 | $this->setModelo($nota->getModelo()); |
|
| 227 | } |
||
| 228 | 5 | $dom = $this->getNode()->ownerDocument; |
|
| 229 | 5 | $dom = $this->validar($dom); |
|
| 230 | 4 | $retorno = $this->envia($dom); |
|
| 231 | 4 | if ($retorno->isAutorizado() && !is_null($nota)) { |
|
| 232 | 2 | $nota->setProtocolo($retorno); |
|
| 233 | } |
||
| 234 | 4 | return $retorno; |
|
| 235 | } |
||
| 236 | |||
| 237 | 5 | public function getNode($name = null) |
|
| 251 | |||
| 252 | 6 | public function loadNode($element, $name = null) |
|
| 253 | { |
||
| 254 | 6 | $name = is_null($name)?'retConsReciNFe':$name; |
|
| 255 | 6 | if ($name == self::INFO_TAGNAME) { |
|
| 256 | 2 | $_fields = $element->getElementsByTagName($name); |
|
| 257 | 2 | if ($_fields->length == 0) { |
|
| 258 | 1 | throw new \Exception('Tag "'.$name.'" não encontrada', 404); |
|
| 259 | } |
||
| 260 | 1 | $element = $_fields->item(0); |
|
| 261 | } else { |
||
| 262 | 4 | $element = parent::loadNode($element, $name); |
|
| 263 | } |
||
| 264 | 5 | $this->setNumero( |
|
| 265 | 5 | Util::loadNode( |
|
| 266 | $element, |
||
| 267 | 5 | 'nRec', |
|
| 268 | 5 | 'Tag "nRec" do campo "Numero" não encontrada' |
|
| 269 | ) |
||
| 270 | ); |
||
| 271 | 5 | $this->setTempoMedio(Util::loadNode($element, 'tMed')); |
|
| 272 | 5 | $this->setCodigo(Util::loadNode($element, 'cMsg')); |
|
| 273 | 5 | $this->setMensagem(Util::loadNode($element, 'xMsg')); |
|
| 274 | 5 | return $element; |
|
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Valida o documento após assinar |
||
| 279 | */ |
||
| 280 | 5 | public function validar($dom) |
|
| 303 | } |
||
| 304 |