Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.35% |
41 / 43 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Ticket action: render staff invoice PDF from client-composed HTML. |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | /** |
| 11 | * Action callable: authorize the staff 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 staff check. |
| 18 | * @param \UppServices\Html2PdfService $html2PdfService HTML-to-PDF renderer service. |
| 19 | * @return array{output: string, contentType: string} |
| 20 | */ |
| 21 | return 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 | $userid = null; |
| 47 | $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid); |
| 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 ticket invoicepdf endpoint."); |
| 55 | $retval = \ApplicationError::Parameters; |
| 56 | } |
| 57 | else { |
| 58 | $html = trim($payload['html']); |
| 59 | if ($html === '') { |
| 60 | $logger->error("Empty HTML payload for ticket invoicepdf 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("Invoice 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 | }; |