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