Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.75% |
44 / 64 |
|
100.00% |
1 / 1 |
CRAP | n/a |
0 / 0 |
|
| ticketsii_async_send_validate_payload | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
13 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | require_once __DIR__.'/sendsii.php'; |
| 6 | |
| 7 | /** |
| 8 | * @param array<string, mixed> $data |
| 9 | * @param-out string|null $device |
| 10 | * @param-out string|null $session |
| 11 | * @param-out string|null $place |
| 12 | * @param-out string|null $audit |
| 13 | */ |
| 14 | function ticketsii_async_send_validate_payload(array $data, ?string &$device, ?string &$session, ?string &$place, ?string &$audit, \Psr\Log\LoggerInterface $logger): int |
| 15 | { |
| 16 | $retval = \ApplicationError::Success; |
| 17 | $device = isset($data['device']) ? (string) $data['device'] : null; |
| 18 | $session = isset($data['session']) ? (string) $data['session'] : null; |
| 19 | $place = isset($data['place']) ? (string) $data['place'] : null; |
| 20 | $audit = isset($data['audit']) ? (string) $data['audit'] : null; |
| 21 | |
| 22 | if ($device === null || $device === '' || $session === null || $session === '' || $place === null || $place === '' || $audit === null || $audit === '') { |
| 23 | $retval = \ApplicationError::Parameters; |
| 24 | $logger->error("Invalid POST contents for ticketsii/async: mandatory fields device, session, place, audit."); |
| 25 | } |
| 26 | |
| 27 | return $retval; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Envío SII en segundo plano (misma lógica que GET /ticketsii/audit o GET /sii/audit), con pausa inicial como TicketBAI/VeriFactu. |
| 32 | * |
| 33 | * @return callable(string, \ConxHelper, \Psr\Log\LoggerInterface): array{output: string, contentType: string} |
| 34 | */ |
| 35 | return function (string $rawBody, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger): array { |
| 36 | $retval = \ApplicationError::Success; |
| 37 | $device = null; |
| 38 | $session = null; |
| 39 | $place = null; |
| 40 | $audit = null; |
| 41 | |
| 42 | if ($rawBody === '') { |
| 43 | $retval = \ApplicationError::Parameters; |
| 44 | $logger->error('Empty request body for ticketsii/async.'); |
| 45 | } |
| 46 | |
| 47 | $data = null; |
| 48 | if (\success($retval)) { |
| 49 | $decoded = json_decode($rawBody, true); |
| 50 | if (! is_array($decoded)) { |
| 51 | $retval = \ApplicationError::Parameters; |
| 52 | $logger->error('Invalid JSON format in ticketsii/async body.'); |
| 53 | } |
| 54 | else { |
| 55 | $data = $decoded; |
| 56 | \addlog(__FILE__, \LogLevel::Debug, 'DUMP OF POST DATA:'.PHP_EOL.print_r($data, true).PHP_EOL); |
| 57 | \addlog(__FILE__, \LogLevel::Debug, "TICKETSII async payload received [bytes: '".strlen($rawBody)."', keys: '".implode(',', array_keys($data))."']"); |
| 58 | $logger->info("TICKETSII async payload received [bytes: '".strlen($rawBody)."', keys: '".implode(',', array_keys($data))."']"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (\success($retval) && $data !== null) { |
| 63 | $retval = ticketsii_async_send_validate_payload($data, $device, $session, $place, $audit, $logger); |
| 64 | } |
| 65 | |
| 66 | if (\success($retval)) { |
| 67 | ini_set('max_execution_time', '3600'); |
| 68 | ini_set('ignore_user_abort', '1'); |
| 69 | sleep(5); |
| 70 | } |
| 71 | |
| 72 | if (\success($retval) && $place !== null && $audit !== null) { |
| 73 | \addlog(__FILE__, \LogLevel::Information, "TICKETSII async request accepted [place: '".$place."', audit: '".$audit."']"); |
| 74 | $logger->info("TICKETSII async request accepted [place: '".$place."', audit: '".$audit."']"); |
| 75 | } |
| 76 | |
| 77 | if (\success($retval)) { |
| 78 | $conx->tenant = $place; |
| 79 | if ($conx->tenant === null) { |
| 80 | $retval = \ApplicationError::Database; |
| 81 | \addlog(__FILE__, \LogLevel::Error, "TICKETSII async: tenant connection not available for place '".$place."'"); |
| 82 | $logger->error("TICKETSII async: tenant connection not available for place '".$place."'"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $ticketsok = []; |
| 87 | $ticketsko = []; |
| 88 | if (\success($retval) && $device !== null && $session !== null && $place !== null && $audit !== null) { |
| 89 | \addlog(__FILE__, \LogLevel::Information, "TICKETSII async processing started [device: '".$device."', audit: '".$audit."']"); |
| 90 | $logger->info("TICKETSII async processing started [device: '".$device."', audit: '".$audit."']"); |
| 91 | |
| 92 | $run = sendsii_run($conx, $device, $session, $place, $audit); |
| 93 | $retval = (int) ($run['errorcode'] ?? \ApplicationError::Generic); |
| 94 | $ticketsok = $run['ticketsok'] ?? []; |
| 95 | $ticketsko = $run['ticketsko'] ?? []; |
| 96 | |
| 97 | $okc = is_countable($ticketsok) ? count($ticketsok) : 0; |
| 98 | $koc = is_countable($ticketsko) ? count($ticketsko) : 0; |
| 99 | \addlog(__FILE__, \LogLevel::Information, "TICKETSII async processing finished [errorcode: '".$retval."', ticketsok: ".$okc.', ticketsko: '.$koc.']'); |
| 100 | $logger->info("TICKETSII async processing finished [errorcode: '".$retval."', ticketsok: ".$okc.', ticketsko: '.$koc.']'); |
| 101 | } |
| 102 | |
| 103 | $result = [ |
| 104 | 'errorcode' => $retval, |
| 105 | 'ticketsok' => $ticketsok, |
| 106 | 'ticketsko' => $ticketsko, |
| 107 | ]; |
| 108 | |
| 109 | return [ 'output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8' ]; |
| 110 | }; |