Skip to content

Commit 65223e6

Browse files
committed
Se ha estandarizado correctamente el código, tanto en el API Client como en los servicios y en los tests. Se hicieron algunas correcciones en la documentación unit-test.rst, example.rst e install.rst.
1 parent 58150d2 commit 65223e6

File tree

53 files changed

+807
-473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+807
-473
lines changed

docs/dev/unit-test.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Ejemplos
2929

3030
Para revisar más ejemplos de cómo consumir los servicios web, dependiendo de la forma de autenticación que requieras usar, revisa las siguientes pruebas:
3131

32-
- Sin autenticación en SII: SiiContribuyentesTest.php.
33-
- Autenticación con RUT y clave tributaria: SiiMisiiTest.php o SiiBheTest.php.
34-
- Autenticación con firma electrónica: SiiDteTest.php.
32+
- Sin autenticación en SII: carpeta ``tests/sii/contribuyentes/``, ``tests/sii/actividades`` o ``tests/sii/indicadores/``.
33+
- Autenticación con RUT y clave tributaria: carpeta ``tests/sii/bhe_emitidas/`` o ``tests/sii/misii/``.
34+
- Autenticación con firma electrónica: carpeta ``tests/sii/dte_emitidos/``.

docs/getting-started/example.rst

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Ejemplo
22
=======
33

4-
Ejemplo de listar BHEs
4+
Ejemplo de emitir BHEs
55
----------------------
66

77
El siguiente es un ejemplo básico de cómo emitir un documento BHE utilizando el cliente de API.
@@ -18,10 +18,7 @@ Para utilizar el cliente de API de API Gateway, deberás tener definido el token
1818
require_once __DIR__ . '/vendor/autoload.php';
1919
2020
# Importaciones del cliente de API de API Gateway
21-
use apigatewaycl\api_client\ApiClient;
22-
23-
# Creación de nueva instancia de cliente de API
24-
$client = new ApiClient();
21+
use apigatewaycl\api_client\sii\BheEmitidas;
2522
2623
# RUT de contribuyente SII, sin puntos y con Dígito Verificador.
2724
$contribuyente_rut = '12345678-9';
@@ -40,6 +37,9 @@ Para utilizar el cliente de API de API Gateway, deberás tener definido el token
4037
],
4138
];
4239
40+
# Creación de nueva instancia de cliente de API
41+
$client = new BheEmitidas($auth);
42+
4343
$datos_bhe = [
4444
'Encabezado' => [
4545
'IdDoc' => [
@@ -68,16 +68,8 @@ Para utilizar el cliente de API de API Gateway, deberás tener definido el token
6868
]
6969
];
7070
71-
# Recurso a consumir.
72-
$resource = '/sii/bhe/emitidas/emitir';
73-
74-
$data = [
75-
'auth' => $auth,
76-
'boleta' => $datos_bhe
77-
];
78-
7971
# Se efectua la solicitud HTTP y se guarda la respuesta.
80-
$response = $client->post(resource: $resource, data: $data);
72+
$response = $client->emitirBhe($datos_bhe);
8173
8274
# Código del response
8375
echo "Status: ".$response->getStatusCode()."\n";

docs/getting-started/install.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Si estás utilizando la versión de GitHub, deberás instalar los paquetes neces
2121
Requerimientos
2222
--------------
2323

24-
- PHP 7.3 o superior.
24+
- PHP 8.3 o superior.
2525

2626
- Extensiones de PHP:
2727
- curl.

src/ApiBase.php

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,51 @@ public function __construct(
5454
/**
5555
* Configura la autenticación específica para la aplicación.
5656
*
57-
* @param array $credenciales Parámetros de autenticación. Puede ser 'pass' o 'cert'.
57+
* @param array $credenciales Parámetros de autenticación. Puede ser
58+
* 'pass' o 'cert'.
5859
* @throws \apigatewaycl\api_client\ApiException
5960
* @return void
6061
*/
61-
private function setupAuth(array $credenciales)
62+
private function setupAuth(array $credenciales): void
6263
{
6364
$tipo = key($credenciales); // Detecta si es 'pass' o 'cert'
6465
$datos = $credenciales[$tipo] ?? [];
6566

66-
$identificador = $datos['rut'] ?? $datos['cert-data'] ?? $datos['file-data'] ?? null;
67-
$clave = $datos['clave'] ?? $datos['pkey-data'] ?? $datos['file-pass'] ?? null;
67+
$identificador = $datos['rut'] ??
68+
$datos['cert-data'] ??
69+
$datos['file-data'] ??
70+
null;
71+
$clave = $datos['clave'] ??
72+
$datos['pkey-data'] ??
73+
$datos['file-pass'] ??
74+
null;
6875

6976
if ($identificador && $clave) {
7077
if ($this->isAuthPass($identificador)) {
71-
$this->auth = ['pass' => ['rut' => $identificador, 'clave' => $clave]];
78+
$this->auth = [
79+
'pass' => [
80+
'rut' => $identificador,
81+
'clave' => $clave,
82+
],
83+
];
7284
} elseif ($this->isAuthCertData($identificador)) {
73-
$this->auth = ['cert' => ['cert-data' => $identificador, 'pkey-data' => $clave]];
85+
$this->auth = [
86+
'cert' => [
87+
'cert-data' => $identificador,
88+
'pkey-data' => $clave,
89+
],
90+
];
7491
} elseif ($this->isAuthFileData($identificador)) {
75-
$this->auth = ['cert' => ['file-data' => $identificador, 'file-pass' => $clave]];
92+
$this->auth = [
93+
'cert' => [
94+
'file-data' => $identificador,
95+
'file-pass' => $clave,
96+
],
97+
];
7698
} else {
77-
throw new ApiException('No se han proporcionado las credenciales de autentificación.');
99+
throw new ApiException(
100+
message: 'No se han proporcionado las credenciales de autentificación.'
101+
);
78102
}
79103
}
80104
}
@@ -102,9 +126,10 @@ private function setupAuth(array $credenciales)
102126
* - abcdefgh-i (caracteres no permitidos)
103127
*
104128
* @param string $rut El RUT a validar.
105-
* @return bool true si el RUT tiene un formato válido,false en caso contrario.
129+
* @return bool true si el RUT tiene un formato válido,false en caso
130+
* contrario.
106131
*/
107-
private function isAuthPass(string $rut)
132+
private function isAuthPass(string $rut): bool
108133
{
109134
if (is_null($rut)) {
110135
return false;
@@ -118,9 +143,10 @@ private function isAuthPass(string $rut)
118143
* Verifica si una cadena es una cadena codificada en Base64 válida.
119144
*
120145
* @param string $firmaElectronicaBase64 La cadena a verificar.
121-
* @return bool true si la cadena es válida en Base64, false en caso contrario.
146+
* @return bool true si la cadena es válida en Base64, false en caso
147+
* contrario.
122148
*/
123-
private function isAuthFileData(string $firmaElectronicaBase64)
149+
private function isAuthFileData(string $firmaElectronicaBase64): bool
124150
{
125151
if (is_null($firmaElectronicaBase64)) {
126152
return false;
@@ -130,7 +156,10 @@ private function isAuthFileData(string $firmaElectronicaBase64)
130156
return false;
131157
}
132158
// Validar Base64
133-
return base64_decode($firmaElectronicaBase64, true) !== false;
159+
return base64_decode(
160+
string: $firmaElectronicaBase64,
161+
strict: true
162+
) !== false;
134163
}
135164

136165
/**
@@ -160,51 +189,73 @@ private function isAuthFileData(string $firmaElectronicaBase64)
160189
* @param string $pemStr La cadena a validar.
161190
* @return bool true si la cadena tiene formato PEM válido, false en caso contrario.
162191
*/
163-
private function isAuthCertData(string $pemStr)
192+
private function isAuthCertData(string $pemStr): bool
164193
{
165194
if (is_null($pemStr)) {
166195
return false;
167196
}
168197
// Expresión regular para validar el formato PEM
169198
$pattern = '/-----BEGIN ([A-Z ]+)-----\s+([A-Za-z0-9+\/=\s]+)-----END \1-----$/m';
170-
if (!preg_match($pattern, trim($pemStr), $matches)) {
199+
if (!preg_match(
200+
$pattern,
201+
trim($pemStr),
202+
$matches
203+
)
204+
) {
171205
return false;
172206
}
173207
// Validar contenido Base64
174-
$base64Content = preg_replace('/\s+/', '', $matches[2]);
208+
$base64Content = preg_replace(
209+
'/\s+/',
210+
'',
211+
$matches[2]
212+
);
175213
return base64_decode($base64Content, true) !== false;
176214
}
177215

178216
/**
179217
* Obtiene la autenticación de tipo 'pass'.
180218
*
181-
* @throws \apigatewaycl\api_client\ApiException Si falta información de autenticación.
219+
* @throws \apigatewaycl\api_client\ApiException Si falta información
220+
* de autenticación.
182221
* @return array Información de autenticación.
183222
*/
184-
protected function getAuthPass()
223+
protected function getAuthPass(): array
185224
{
186225
if (isset($this->auth['pass'])) {
187226
if (empty($this->auth['pass']['rut'])) {
188-
throw new ApiException('auth.pass.rut empty.');
227+
throw new ApiException(message: 'auth.pass.rut empty.');
189228
}
190229
if (empty($this->auth['pass']['clave'])) {
191-
throw new ApiException('auth.pass.clave empty.');
230+
throw new ApiException(message: 'auth.pass.clave empty.');
192231
}
193232
} elseif (isset($this->auth['cert'])) {
194-
if (isset($this->auth['cert']['cert-data']) && empty($this->auth['cert']['cert-data'])) {
195-
throw new ApiException('auth.cert.cert-data empty.');
233+
if (
234+
isset($this->auth['cert']['cert-data']) &&
235+
empty($this->auth['cert']['cert-data'])
236+
) {
237+
throw new ApiException(message: 'auth.cert.cert-data empty.');
196238
}
197-
if (isset($this->auth['cert']['pkey-data']) && empty($this->auth['cert']['pkey-data'])) {
198-
throw new ApiException('auth.cert.pkey-data empty.');
239+
if (
240+
isset($this->auth['cert']['pkey-data']) &&
241+
empty($this->auth['cert']['pkey-data'])
242+
) {
243+
throw new ApiException(message: 'auth.cert.pkey-data empty.');
199244
}
200-
if (isset($this->auth['cert']['file-data']) && empty($this->auth['cert']['file-data'])) {
201-
throw new ApiException('auth.cert.file-data empty.');
245+
if (
246+
isset($this->auth['cert']['file-data']) &&
247+
empty($this->auth['cert']['file-data'])
248+
) {
249+
throw new ApiException(message: 'auth.cert.file-data empty.');
202250
}
203-
if (isset($this->auth['cert']['file-pass']) && empty($this->auth['cert']['file-pass'])) {
204-
throw new ApiException('auth.cert.file-pass empty.');
251+
if (
252+
isset($this->auth['cert']['file-pass']) &&
253+
empty($this->auth['cert']['file-pass'])
254+
) {
255+
throw new ApiException(message: 'auth.cert.file-pass empty.');
205256
}
206257
} else {
207-
throw new ApiException('auth.pass or auth.cert missing.');
258+
throw new ApiException(message: 'auth.pass or auth.cert missing.');
208259
}
209260

210261
return $this->auth;

0 commit comments

Comments
 (0)