Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
42.86% |
24 / 56 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| verifactu_async_send_validate_payload | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
156 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | require_once __DIR__.'/../legacy/model/spec/verifactu.php'; |
| 6 | |
| 7 | /** |
| 8 | * Validate incoming payload for VeriFactu async processing. |
| 9 | * |
| 10 | * @param array<string, mixed> $data Decoded JSON payload. |
| 11 | * @param string|null $operation Output operation. |
| 12 | * @param string|null $session Output session id. |
| 13 | * @param string|null $place Output place id. |
| 14 | * @param array<string, mixed>|null $entrynfo Output entry data. |
| 15 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 16 | * @return int ApplicationError code. |
| 17 | */ |
| 18 | function verifactu_async_send_validate_payload(array $data, ?string &$operation, ?string &$session, ?string &$place, ?array &$entrynfo, \Psr\Log\LoggerInterface $logger): int |
| 19 | { |
| 20 | $retval = \ApplicationError::Success; |
| 21 | $operation = isset($data['operation']) ? (string) $data['operation'] : null; |
| 22 | $session = isset($data['session']) ? (string) $data['session'] : null; |
| 23 | $place = isset($data['place']) ? (string) $data['place'] : null; |
| 24 | $entrynfo = (isset($data['entrynfo']) && is_array($data['entrynfo'])) ? $data['entrynfo'] : null; |
| 25 | |
| 26 | if (empty($operation) || empty($session) || empty($place) || $entrynfo === null) { |
| 27 | $retval = \ApplicationError::Parameters; |
| 28 | $logger->error("Invalid POST contents for verifactu/async: mandatory fields are missing."); |
| 29 | } |
| 30 | |
| 31 | if (success($retval) && !in_array($operation, [ 'do_insert', 'do_update' ], true)) { |
| 32 | $retval = \ApplicationError::Parameters; |
| 33 | $logger->error("Invalid operation for VeriFactu async request: '".$operation."'"); |
| 34 | } |
| 35 | |
| 36 | return $retval; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Action callable: process async VeriFactu update request. |
| 41 | * |
| 42 | * @param string $rawBody Raw JSON body. |
| 43 | * @param \ConxHelper $conx Connection helper. |
| 44 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 45 | * @return array{output: string, contentType: string} |
| 46 | */ |
| 47 | return function (string $rawBody, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger): array { |
| 48 | $retval = \ApplicationError::Success; |
| 49 | $operation = null; |
| 50 | $session = null; |
| 51 | $place = null; |
| 52 | $entrynfo = null; |
| 53 | |
| 54 | if ($rawBody === '') { |
| 55 | $retval = \ApplicationError::Parameters; |
| 56 | $logger->error("Empty request body for verifactu/async."); |
| 57 | } |
| 58 | |
| 59 | $data = null; |
| 60 | if (success($retval)) { |
| 61 | $decoded = json_decode($rawBody, true); |
| 62 | if (!is_array($decoded)) { |
| 63 | $retval = \ApplicationError::Parameters; |
| 64 | $logger->error("Invalid JSON format in verifactu/async body."); |
| 65 | } |
| 66 | else { |
| 67 | $data = $decoded; |
| 68 | \addlog(__FILE__, \LogLevel::Debug, "DUMP OF POST DATA:".PHP_EOL.print_r($data, true).PHP_EOL); |
| 69 | \addlog(__FILE__, \LogLevel::Debug, "VERIFACTU async payload received [bytes: '".strlen($rawBody)."', keys: '".implode(',', array_keys($data))."']"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (success($retval) && $data !== null) { |
| 74 | $retval = verifactu_async_send_validate_payload($data, $operation, $session, $place, $entrynfo, $logger); |
| 75 | } |
| 76 | |
| 77 | if (success($retval)) { |
| 78 | ini_set('max_execution_time', '3600'); |
| 79 | ini_set('ignore_user_abort', '1'); |
| 80 | sleep(5); // pause before processing to allow synchronize to complete |
| 81 | } |
| 82 | |
| 83 | if (success($retval) && $entrynfo !== null && isset($entrynfo['objid'])) { |
| 84 | \addlog(__FILE__, \LogLevel::Information, "VERIFACTU async request accepted [operation: '".$operation."', place: '".$place."', entry: '".$entrynfo['objid']."']"); |
| 85 | } |
| 86 | |
| 87 | if (success($retval)) { |
| 88 | $conx->tenant = $place; |
| 89 | if ($conx->tenant === null) { |
| 90 | $retval = \ApplicationError::Database; |
| 91 | \addlog(__FILE__, \LogLevel::Error, "Tenant connection not available for place '".$place."'"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (success($retval) && $entrynfo !== null && isset($entrynfo['objid'])) { |
| 96 | \addlog(__FILE__, \LogLevel::Information, "VERIFACTU async processing started [operation: '".$operation."', entry: '".$entrynfo['objid']."']"); |
| 97 | $retval = \verifactu_enqueue_async_entry($conx, $place, $entrynfo); |
| 98 | } |
| 99 | |
| 100 | if (success($retval)) { |
| 101 | $retval = \verifactu_process_queue($conx, $session, $place); |
| 102 | } |
| 103 | |
| 104 | if ($entrynfo !== null && isset($entrynfo['objid'])) { |
| 105 | \addlog(__FILE__, \LogLevel::Information, "VERIFACTU async processing finished [entry: '".$entrynfo['objid']."', valid: '".($entrynfo['valid'] ?? '')."', error: '".($entrynfo['error'] ?? '')."', errorcode: '".$retval."']"); |
| 106 | } |
| 107 | |
| 108 | $result = [ |
| 109 | 'errorcode' => $retval, |
| 110 | ]; |
| 111 | |
| 112 | return [ 'output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8' ]; |
| 113 | }; |
| 114 |