Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
83.33% covered (success)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TicketbaiService
96.00% covered (success)
96.00%
24 / 25
83.33% covered (success)
83.33%
5 / 6
9
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 runAction
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 authorize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 derived
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 async
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 audit
83.33% covered (success)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7require_once __DIR__.'/legacy/ddbb/conx.php';
8
9/**
10 * TicketBAI signing material actions.
11 * Delegates to action files in App/Services/ticketbai/.
12 */
13class TicketbaiService
14{
15    private const TICKETBAI_ACTIONS_DIR = __DIR__.'/ticketbai';
16
17    private readonly SessionService $sessionService;
18    private readonly LoggerFactory $loggerFactory;
19
20    /** @var array<string, callable> */
21    private static array $actionCache = [];
22    /** @var callable|null */
23    private static $asyncAction = null;
24
25    public function __construct(
26        SessionService $sessionService,
27        LoggerFactory $loggerFactory,
28    ) {
29        $this->sessionService = $sessionService;
30        $this->loggerFactory = $loggerFactory;
31    }
32
33    /**
34     * Load and run a TicketBAI action with ConxHelper lifecycle managed here.
35     *
36     * @param string $actionName Action file name (without .php).
37     * @param array<string, mixed> $query Query params.
38     * @return array{output: string, contentType: string}
39     */
40    private function runAction(string $actionName, array $query): array
41    {
42        $conx = new \ConxHelper(__FILE__);
43        if (!isset(self::$actionCache[$actionName])) {
44            $path = self::TICKETBAI_ACTIONS_DIR.'/'.$actionName.'.php';
45            self::$actionCache[$actionName] = require_once $path;
46        }
47        $action = self::$actionCache[$actionName];
48        $logger = $this->loggerFactory->for(__FILE__);
49        try {
50            return $action($query, $conx, $logger, $this->sessionService);
51        } finally {
52            $conx->release();
53        }
54    }
55
56    /**
57     * GET /ticketbai/authorize.
58     *
59     * @param array<string, mixed> $query Query params.
60     * @return array{output: string, contentType: string}
61     */
62    public function authorize(array $query): array
63    {
64        return $this->runAction('Authorize', $query);
65    }
66
67    /**
68     * GET /ticketbai/derived.
69     *
70     * @param array<string, mixed> $query Query params.
71     * @return array{output: string, contentType: string}
72     */
73    public function derived(array $query): array
74    {
75        return $this->runAction('Derived', $query);
76    }
77
78    /**
79     * POST /ticketbai/async.
80     *
81     * @param string $rawBody Raw request body (JSON).
82     * @return array{output: string, contentType: string}
83     */
84    public function async(string $rawBody): array
85    {
86        $conx = new \ConxHelper(__FILE__);
87        if (self::$asyncAction === null) {
88            self::$asyncAction = require_once self::TICKETBAI_ACTIONS_DIR.'/AsyncSend.php';
89        }
90        $action = self::$asyncAction;
91        $logger = $this->loggerFactory->for(__FILE__);
92        try {
93            return $action($rawBody, $conx, $logger);
94        } finally {
95            $conx->release();
96        }
97    }
98
99    /**
100     * GET /ticketbai/audit — retry pending/failed TicketBAI rows for one audit period.
101     *
102     * @param array<string, string|int|float|null> $query
103     * @return array{errorcode: int, ticketsok: mixed, ticketsko: mixed}
104     */
105    public function audit(array $query): array
106    {
107        $conx = new \ConxHelper(__FILE__);
108        if (!function_exists('ticketbai_audit_retry_run')) {
109            require_once self::TICKETBAI_ACTIONS_DIR.'/AuditRetry.php';
110        }
111        $logger = $this->loggerFactory->for(__FILE__);
112        try {
113            return \ticketbai_audit_retry_run($query, $conx, $logger, $this->sessionService);
114        } finally {
115            $conx->release();
116        }
117    }
118}
119