Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.67% |
42 / 43 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GuestService | |
97.67% |
42 / 43 |
|
83.33% |
5 / 6 |
9 | |
0.00% |
0 / 1 |
| __construct | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
1.00 | |||
| runAction | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| access | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| check | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ticketpdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| invoicesend | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace UppServices; |
| 6 | |
| 7 | /** |
| 8 | * Guest table-access and courtesy ticket PDF actions. Delegates to action files in App/Services/guest/. |
| 9 | * Each action receives a ConxHelper (connection created lazily on first use) and $logger. |
| 10 | * Html2PdfService is injected into the Ticketpdf action only. |
| 11 | * Invoice send reuses ticket/InvoiceSend.php with authMode=guest. |
| 12 | */ |
| 13 | class GuestService |
| 14 | { |
| 15 | private const GUEST_ACTIONS_DIR = __DIR__.'/guest'; |
| 16 | private readonly LoggerFactory $loggerFactory; |
| 17 | private readonly SessionService $sessionService; |
| 18 | private readonly Html2PdfService $html2PdfService; |
| 19 | |
| 20 | /** @var callable */ |
| 21 | private $emailSend; |
| 22 | |
| 23 | public function __construct( |
| 24 | LoggerFactory $loggerFactory, |
| 25 | SessionService $sessionService, |
| 26 | Html2PdfService $html2PdfService, |
| 27 | ?callable $emailSend = null, |
| 28 | ) { |
| 29 | $this->loggerFactory = $loggerFactory; |
| 30 | $this->sessionService = $sessionService; |
| 31 | $this->html2PdfService = $html2PdfService; |
| 32 | require_once __DIR__.'/../Legacy/mailing/mailing.php'; |
| 33 | $this->emailSend = $emailSend ?? static function ( |
| 34 | string &$html, |
| 35 | string $from, |
| 36 | string $to, |
| 37 | string $subject, |
| 38 | array &$attachments = [] |
| 39 | ): bool { |
| 40 | return email_send($html, $from, $to, $subject, $attachments); |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | /** @var array<string, callable> cache of loaded action callables to avoid redeclaring functions on second require */ |
| 45 | private static array $actionCache = []; |
| 46 | |
| 47 | /** |
| 48 | * Load and run an action. Creates a ConxHelper and passes it to the action; connection is created lazily by the helper; caller releases it. |
| 49 | * |
| 50 | * @param array<string, mixed> $query |
| 51 | * @return array{output: string, contentType: string} |
| 52 | */ |
| 53 | private function runAction(string $actionName, string $body, array $query): array |
| 54 | { |
| 55 | require_once __DIR__.'/legacy/ddbb/conx.php'; |
| 56 | $conx = new \ConxHelper(__FILE__); |
| 57 | |
| 58 | if (!isset(self::$actionCache[$actionName])) { |
| 59 | $path = self::GUEST_ACTIONS_DIR.'/'.$actionName.'.php'; |
| 60 | self::$actionCache[$actionName] = require $path; |
| 61 | } |
| 62 | $action = self::$actionCache[$actionName]; |
| 63 | $logger = $this->loggerFactory->for(__FILE__); |
| 64 | try { |
| 65 | if ($actionName === 'Ticketpdf') { |
| 66 | return $action($body, $query, $conx, $logger, $this->sessionService, $this->html2PdfService); |
| 67 | } |
| 68 | return $action($body, $query, $conx, $logger, $this->sessionService); |
| 69 | } |
| 70 | finally { |
| 71 | $conx->release(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** POST /guest/access - grant guest session by table QR */ |
| 76 | public function access(string $body, array $query): array |
| 77 | { |
| 78 | return $this->runAction('Access', $body, $query); |
| 79 | } |
| 80 | |
| 81 | /** GET /guest/check - verify guest session */ |
| 82 | public function check(array $query): array |
| 83 | { |
| 84 | return $this->runAction('Check', '', $query); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * POST /guest/ticketpdf - render courtesy ticket PDF from HTML. |
| 89 | * |
| 90 | * @param string $body JSON payload with html. |
| 91 | * @param array<string, mixed> $query |
| 92 | * @return array{output: string, contentType: string} |
| 93 | */ |
| 94 | public function ticketpdf(string $body, array $query): array |
| 95 | { |
| 96 | return $this->runAction('Ticketpdf', $body, $query); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * POST /guest/invoicesend - send invoice email (PDF + optional Factura-e) for guest session. |
| 101 | * |
| 102 | * @param string $body JSON with ticket, html, email. |
| 103 | * @param array<string, mixed> $query |
| 104 | * @return array{output: string, contentType: string} |
| 105 | */ |
| 106 | public function invoicesend(string $body, array $query): array |
| 107 | { |
| 108 | require_once __DIR__.'/legacy/ddbb/conx.php'; |
| 109 | $conx = new \ConxHelper(__FILE__); |
| 110 | $logger = $this->loggerFactory->for(__FILE__); |
| 111 | if (!isset(self::$actionCache['InvoiceSend'])) { |
| 112 | self::$actionCache['InvoiceSend'] = require __DIR__.'/ticket/InvoiceSend.php'; |
| 113 | } |
| 114 | $action = self::$actionCache['InvoiceSend']; |
| 115 | try { |
| 116 | return $action( |
| 117 | $body, |
| 118 | $query, |
| 119 | $conx, |
| 120 | $logger, |
| 121 | $this->sessionService, |
| 122 | $this->emailSend, |
| 123 | 'guest' |
| 124 | ); |
| 125 | } |
| 126 | finally { |
| 127 | $conx->release(); |
| 128 | } |
| 129 | } |
| 130 | } |