Compare commits
2 Commits
b8f210478e
...
3ca44ea26b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ca44ea26b | ||
|
|
53f451c54c |
@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\PadronEstatalException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PadronEstatalService
|
||||
{
|
||||
@ -33,6 +34,8 @@ public function getVehiculoByFolio(string $folio): array
|
||||
*/
|
||||
private function consultarPadron(string $tipo, string $valor): array
|
||||
{
|
||||
$logger = Log::channel('padron_estatal');
|
||||
|
||||
// Construir el Data en formato JSON
|
||||
$data = json_encode([
|
||||
'tipo' => $tipo,
|
||||
@ -51,6 +54,13 @@ private function consultarPadron(string $tipo, string $valor): array
|
||||
</soapenv:Envelope>
|
||||
XML;
|
||||
|
||||
$logger->info('Consulta al padrón estatal', [
|
||||
'url' => $this->soapUrl,
|
||||
'tipo' => $tipo,
|
||||
'valor' => $valor,
|
||||
]);
|
||||
$logger->debug('SOAP request body', ['body' => $soapBody]);
|
||||
|
||||
// Configurar cURL
|
||||
$ch = curl_init($this->soapUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
@ -68,16 +78,32 @@ private function consultarPadron(string $tipo, string $valor): array
|
||||
// Ejecutar la petición
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$curlInfo = curl_getinfo($ch);
|
||||
$error = curl_error($ch);
|
||||
|
||||
$logger->debug('Respuesta cURL', [
|
||||
'http_code' => $httpCode,
|
||||
'total_time' => $curlInfo['total_time'] ?? null,
|
||||
'connect_time' => $curlInfo['connect_time'] ?? null,
|
||||
'namelookup_time' => $curlInfo['namelookup_time'] ?? null,
|
||||
'curl_error' => $error ?: null,
|
||||
]);
|
||||
|
||||
if ($error) {
|
||||
$logger->error('Error cURL al consultar padrón estatal', ['error' => $error]);
|
||||
throw new PadronEstatalException("Error en la petición al padrón estatal: {$error}");
|
||||
}
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
$logger->error('HTTP error al consultar padrón estatal', [
|
||||
'http_code' => $httpCode,
|
||||
'response' => $response,
|
||||
]);
|
||||
throw new PadronEstatalException("Error HTTP {$httpCode} al consultar padrón estatal");
|
||||
}
|
||||
|
||||
$logger->debug('SOAP response body', ['body' => $response]);
|
||||
|
||||
// Parsear la respuesta
|
||||
return $this->parsearRespuesta($response);
|
||||
} finally {
|
||||
@ -90,10 +116,13 @@ private function consultarPadron(string $tipo, string $valor): array
|
||||
*/
|
||||
private function parsearRespuesta(string $soapResponse): array
|
||||
{
|
||||
$logger = Log::channel('padron_estatal');
|
||||
|
||||
// Extraer el contenido del tag
|
||||
preg_match('/<result>(.*?)<\/result>/s', $soapResponse, $matches);
|
||||
|
||||
if (!isset($matches[1])) {
|
||||
$logger->error('No se encontró tag <result> en la respuesta SOAP', ['response' => $soapResponse]);
|
||||
throw new PadronEstatalException("No se pudo extraer el resultado del padrón estatal");
|
||||
}
|
||||
|
||||
@ -103,11 +132,16 @@ private function parsearRespuesta(string $soapResponse): array
|
||||
$result = json_decode($jsonContent, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
$logger->error('Error al decodificar JSON del padrón estatal', [
|
||||
'json_error' => json_last_error_msg(),
|
||||
'content' => $jsonContent,
|
||||
]);
|
||||
throw new PadronEstatalException("Error al decodificar JSON del padrón estatal: " . json_last_error_msg());
|
||||
}
|
||||
|
||||
// La respuesta es un array con un objeto que tiene error y datos
|
||||
if (!isset($result[0])) {
|
||||
$logger->error('Formato de respuesta inesperado (sin índice 0)', ['result' => $result]);
|
||||
throw new PadronEstatalException("Formato de respuesta inesperado del padrón estatal");
|
||||
}
|
||||
|
||||
@ -115,14 +149,21 @@ private function parsearRespuesta(string $soapResponse): array
|
||||
|
||||
// Verificar si hay error
|
||||
if ($data['error'] !== 0) {
|
||||
$logger->warning('El padrón estatal devolvió un código de error', [
|
||||
'error_code' => $data['error'],
|
||||
'data' => $data,
|
||||
]);
|
||||
throw new PadronEstatalException("Error en consulta al padrón estatal: código {$data['error']}");
|
||||
}
|
||||
|
||||
// Verificar si hay datos
|
||||
if (!isset($data['datos'][0])) {
|
||||
$logger->warning('El padrón estatal no devolvió datos del vehículo', ['data' => $data]);
|
||||
throw new PadronEstatalException("No se encontraron datos del vehículo en el padrón estatal");
|
||||
}
|
||||
|
||||
$logger->info('Consulta al padrón estatal exitosa');
|
||||
|
||||
return $data['datos'][0];
|
||||
}
|
||||
|
||||
|
||||
@ -135,5 +135,13 @@
|
||||
'driver' => 'single',
|
||||
'path' => storage_path('logs/debug.log'),
|
||||
],
|
||||
|
||||
'padron_estatal' => [
|
||||
'driver' => 'daily',
|
||||
'path' => storage_path('logs/padron-estatal.log'),
|
||||
'level' => 'debug',
|
||||
'days' => 14,
|
||||
'replace_placeholders' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use App\Models\PermissionType;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$apk = PermissionType::firstOrCreate(['name' => 'App Móvil']);
|
||||
|
||||
Permission::firstOrCreate(
|
||||
['name' => 'apk.index'],
|
||||
['guard_name' => 'api', 'description' => 'Ver lista de versiones del apk', 'permission_type_id' => $apk->id]
|
||||
);
|
||||
|
||||
$apk = PermissionType::firstOrCreate(['name' => 'Cargar Apk']);
|
||||
|
||||
Permission::firstOrCreate(
|
||||
['name' => 'apk.create'],
|
||||
['guard_name' => 'api', 'description' => 'Crear nueva versión del apk', 'permission_type_id' => $apk->id]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Permission::whereIn('name', ['apk.index', 'apk.create'])->delete();
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user