Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.51% covered (danger)
41.51%
22 / 53
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
ticketbai_async_send_validate_payload
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3declare(strict_types=1);
4
5require_once __DIR__.'/../legacy/model/spec/ticketbai.php';
6
7/**
8 * Validate incoming payload for TicketBAI 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 */
18function ticketbai_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 ticketbai/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 TicketBAI async request: '".$operation."'");
34    }
35
36    return $retval;
37}
38
39/**
40 * Action callable: process async TicketBAI 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 */
47return 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 ticketbai/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 ticketbai/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, "TICKETBAI async payload received [bytes: '".strlen($rawBody)."', keys: '".implode(',', array_keys($data))."']");
70        }
71    }
72
73    if (success($retval) && $data !== null) {
74        $retval = ticketbai_async_send_validate_payload($data, $operation, $session, $place, $entrynfo, $logger);
75    }
76
77    if (success($retval)) {
78        ini_set('max_execution_time', '120');
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, "TICKETBAI 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, "TICKETBAI async processing started [operation: '".$operation."', entry: '".$entrynfo['objid']."']");
97        $retval = \update_ticketbai_request($conx, $session, $entrynfo);
98        \addlog(__FILE__, \LogLevel::Information, "TICKETBAI async processing finished [entry: '".$entrynfo['objid']."', valid: '".$entrynfo['valid']."', error: '".($entrynfo['error'] ?? '')."', errorcode: '".$retval."']");
99    }
100
101    $result = [
102        'errorcode' => $retval,
103    ];
104
105    return [ 'output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8' ];
106};
107