Conditions | 3 |
Paths | 3 |
Total Lines | 68 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 12 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
75 | public function validate( |
||
76 | SiiRequestInterface $request, |
||
77 | string $company, |
||
78 | int $document, |
||
79 | int $number, |
||
80 | string $date, |
||
81 | int $total, |
||
82 | string $recipient, |
||
83 | string $signature |
||
84 | ): SiiValidateDocumentSignatureResponse { |
||
85 | // Validar los RUT que se utilizarán para la consulta de estado del DTE. |
||
86 | Rut::validate($company); |
||
87 | Rut::validate($recipient); |
||
88 | [$rutCompany, $dvCompany] = Rut::toArray($company); |
||
89 | [$rutRecipient, $dvRecipient] = Rut::toArray($recipient); |
||
90 | |||
91 | // Validar fecha y convertir al formato del SII. |
||
92 | $dateSii = Date::validateAndConvert($date, 'dmY'); |
||
93 | if ($dateSii === null) { |
||
94 | throw new SiiValidateDocumentSignatureException(sprintf( |
||
95 | 'La fecha %s del documento no es válida, debe tener formato AAAA-MM-DD.', |
||
96 | $date |
||
97 | )); |
||
98 | } |
||
99 | |||
100 | // Obtener el token asociado al certificado digital. |
||
101 | $token = $this->authenticateJob->authenticate($request); |
||
102 | |||
103 | // Datos para la consulta. |
||
104 | $requestData = [ |
||
105 | 'RutEmpresa' => $rutCompany, |
||
106 | 'DvEmpresa' => $dvCompany, |
||
107 | 'RutReceptor' => $rutRecipient, |
||
108 | 'DvReceptor' => $dvRecipient, |
||
109 | 'TipoDte' => $document, |
||
110 | 'FolioDte' => $number, |
||
111 | 'FechaEmisionDte' => $dateSii, |
||
112 | 'MontoDte' => $total, |
||
113 | 'FirmaDte' => $signature, |
||
114 | 'Token' => $token, |
||
115 | ]; |
||
116 | |||
117 | // Consultar el estado del documento, incluyendo su firma, al SII. |
||
118 | try { |
||
119 | $xmlResponse = $this->consumeWebserviceJob->sendRequest( |
||
120 | $request, |
||
121 | 'QueryEstDteAv', |
||
122 | 'getEstDteAv', |
||
123 | $requestData |
||
124 | ); |
||
125 | } catch (SiiConsumeWebserviceException $e) { |
||
126 | throw new SiiValidateDocumentSignatureException(sprintf( |
||
127 | 'No fue posible obtener el estado de la firma del documento T%dF%d de %d-%s desde el SII. %s', |
||
128 | $document, |
||
129 | $number, |
||
130 | $rutCompany, |
||
131 | $dvCompany, |
||
132 | $e->getMessage() |
||
133 | )); |
||
134 | } |
||
135 | |||
136 | // Armar estado del XML enviado y retornar. |
||
137 | $responseData = $this->xmlComponent->getDecoderWorker()->decode( |
||
138 | $xmlResponse |
||
139 | ); |
||
140 | return new SiiValidateDocumentSignatureResponse( |
||
141 | $responseData, |
||
142 | $requestData |
||
143 | ); |
||
146 |