Passed
Push — master ( 9d79d3...4bb85e )
by Esteban De La Fuente
08:41
created

ValidateDocumentSignatureJob::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 68
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 68
c 1
b 0
f 0
ccs 0
cts 45
cp 0
rs 9.28
cc 3
nc 3
nop 8
crap 12

How to fix   Long Method    Many Parameters   

Long Method

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:

Many Parameters

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
2
3
declare(strict_types=1);
4
5
/**
6
 * LibreDTE: Biblioteca PHP (Núcleo).
7
 * Copyright (C) LibreDTE <https://www.libredte.cl>
8
 *
9
 * Este programa es software libre: usted puede redistribuirlo y/o modificarlo
10
 * bajo los términos de la Licencia Pública General Affero de GNU publicada por
11
 * la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o
12
 * (a su elección) cualquier versión posterior de la misma.
13
 *
14
 * Este programa se distribuye con la esperanza de que sea útil, pero SIN
15
 * GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD
16
 * PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública
17
 * General Affero de GNU para obtener una información más detallada.
18
 *
19
 * Debería haber recibido una copia de la Licencia Pública General Affero de
20
 * GNU junto a este programa.
21
 *
22
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
23
 */
24
25
namespace libredte\lib\Core\Package\Billing\Component\Integration\Worker\SiiLazy\Job;
26
27
use Derafu\Lib\Core\Foundation\Abstract\AbstractJob;
28
use Derafu\Lib\Core\Foundation\Contract\JobInterface;
29
use Derafu\Lib\Core\Helper\Date;
30
use Derafu\Lib\Core\Helper\Rut;
31
use Derafu\Lib\Core\Package\Prime\Component\Xml\Contract\XmlComponentInterface;
32
use libredte\lib\Core\Package\Billing\Component\Integration\Contract\SiiRequestInterface;
33
use libredte\lib\Core\Package\Billing\Component\Integration\Exception\SiiConsumeWebserviceException;
34
use libredte\lib\Core\Package\Billing\Component\Integration\Exception\SiiValidateDocumentSignatureException;
35
use libredte\lib\Core\Package\Billing\Component\Integration\Support\Response\SiiValidateDocumentSignatureResponse;
36
37
/**
38
 * Clase para el envío de documentos al SII.
39
 *
40
 * Principalmente es para el envío y consulta de estado del envío de documentos
41
 * tributarios electrónicos en formato XML.
42
 */
43
class ValidateDocumentSignatureJob extends AbstractJob implements JobInterface
44
{
45
    public function __construct(
46
        private AuthenticateJob $authenticateJob,
47
        private ConsumeWebserviceJob $consumeWebserviceJob,
48
        private XmlComponentInterface $xmlComponent
49
    ) {
50
    }
51
52
    /**
53
     * Obtiene el estado avanzado de un documento en el SII.
54
     *
55
     * Este estado solo se obtiene si el documento se encuentra aceptado por el
56
     * SII, ya sea aceptado 100% OK o con reparos.
57
     *
58
     * Este servicio valida que el documento exista en SII (esté aceptado), que
59
     * los datos del documento proporcionados coincidan. Finalmente, valida que
60
     * la firma electrónica del documento coincida con la enviada al SII.
61
     *
62
     * Referencia: https://www.sii.cl/factura_electronica/factura_mercado/OIFE2006_QueryEstDteAv_MDE.pdf
63
     *
64
     * @param SiiRequestInterface $request Datos de la solicitud al SII.
65
     * @param string $company RUT de la empresa emisora del documento.
66
     * @param int $document Tipo de documento tributario electrónico.
67
     * @param int $number Folio del documento.
68
     * @param string $date Fecha de emisión del documento, formato: AAAA-MM-DD.
69
     * @param int $total Total del documento.
70
     * @param string $recipient RUT del receptor del documento.
71
     * @param string $signature Tag DTE/Signature/SignatureValue del XML.
72
     * @return SiiValidateDocumentSignatureResponse
73
     * @throws SiiValidateDocumentSignatureException En caso de error.
74
     */
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
        );
144
    }
145
}
146