Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.35% covered (success)
95.35%
41 / 43
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3/**
4 * @file
5 * Guest action: render a courtesy ticket PDF from client-composed HTML.
6 */
7
8declare(strict_types=1);
9
10/**
11 * Action callable: authorize the guest session and render HTML into a PDF.
12 *
13 * @param string $body JSON payload: { html: string }.
14 * @param array<string, mixed> $query Query params: device, session.
15 * @param \ConxHelper $conx Connection helper.
16 * @param \Psr\Log\LoggerInterface $logger Logger.
17 * @param \UppServices\SessionService $sessionService Session service for guest check.
18 * @param \UppServices\Html2PdfService $html2PdfService HTML-to-PDF renderer service.
19 * @return array{output: string, contentType: string}
20 */
21return function (
22    string $body,
23    array $query,
24    \ConxHelper $conx,
25    \Psr\Log\LoggerInterface $logger,
26    \UppServices\SessionService $sessionService,
27    \UppServices\Html2PdfService $html2PdfService
28): array {
29    $retval = \ApplicationError::Success;
30    $html = '';
31    $pdfdata = null;
32
33    $device = $query['device'] ?? null;
34    if (!$device) {
35        $logger->error("No device provided on URL. Operation cancelled.");
36        $retval = \ApplicationError::Parameters;
37    }
38
39    if (\success($retval)) {
40        $session = $query['session'] ?? null;
41        if (!$session) {
42            $logger->error("No session provided on URL. Operation cancelled.");
43            $retval = \ApplicationError::Parameters;
44        }
45        else {
46            $qrscanid = null;
47            $retval = $sessionService->checkAccess($conx, (string) $session, (string) $device, $qrscanid);
48        }
49    }
50
51    if (\success($retval)) {
52        $payload = json_decode($body, true);
53        if (!\is_array($payload) || !isset($payload['html']) || !\is_string($payload['html'])) {
54            $logger->error("Invalid JSON payload for guest ticketpdf endpoint.");
55            $retval = \ApplicationError::Parameters;
56        }
57        else {
58            $html = trim($payload['html']);
59            if ($html === '') {
60                $logger->error("Empty HTML payload for guest ticketpdf endpoint.");
61                $retval = \ApplicationError::Parameters;
62            }
63        }
64    }
65
66    if (\success($retval)) {
67        try {
68            $pdfcontent = $html2PdfService->render($html, 'A4', 'portrait');
69            $pdfdata = base64_encode($pdfcontent);
70        }
71        catch (\Throwable $e) {
72            $logger->error("Ticket PDF generation failed: ".$e->getMessage());
73            $retval = \ApplicationError::Unknown;
74        }
75    }
76
77    $result = [
78        "errorcode" => $retval,
79        "pdfdata" => \success($retval) ? $pdfdata : null,
80    ];
81    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
82};