Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
34 / 36 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TicketService | |
94.12% |
32 / 34 |
|
50.00% |
2 / 4 |
7.01 | |
0.00% |
0 / 1 |
| __construct | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
1.00 | |||
| runAction | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
4 | |||
| invoicepdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| invoicesend | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace UppServices; |
| 6 | |
| 7 | require_once __DIR__.'/legacy/ddbb/conx.php'; |
| 8 | require_once __DIR__.'/../Legacy/mailing/mailing.php'; |
| 9 | |
| 10 | /** |
| 11 | * Staff ticket invoice PDF and email-send actions. |
| 12 | * Delegates to action files in App/Services/ticket/. |
| 13 | * Html2PdfService is injected into Invoicepdf; email sender into InvoiceSend. |
| 14 | */ |
| 15 | class TicketService |
| 16 | { |
| 17 | private const TICKET_ACTIONS_DIR = __DIR__.'/ticket'; |
| 18 | |
| 19 | private readonly SessionService $sessionService; |
| 20 | private readonly LoggerFactory $loggerFactory; |
| 21 | private readonly Html2PdfService $html2PdfService; |
| 22 | |
| 23 | /** @var callable */ |
| 24 | private $emailSend; |
| 25 | |
| 26 | /** @var array<string, callable> */ |
| 27 | private static array $actionCache = []; |
| 28 | |
| 29 | /** |
| 30 | * @param callable|null $emailSend Optional mail sender for tests: |
| 31 | * function (string &$html, string $from, string $to, string $subject, array &$attachments = []): bool |
| 32 | */ |
| 33 | public function __construct( |
| 34 | SessionService $sessionService, |
| 35 | LoggerFactory $loggerFactory, |
| 36 | Html2PdfService $html2PdfService, |
| 37 | ?callable $emailSend = null, |
| 38 | ) { |
| 39 | $this->sessionService = $sessionService; |
| 40 | $this->loggerFactory = $loggerFactory; |
| 41 | $this->html2PdfService = $html2PdfService; |
| 42 | $this->emailSend = $emailSend ?? static function ( |
| 43 | string &$html, |
| 44 | string $from, |
| 45 | string $to, |
| 46 | string $subject, |
| 47 | array &$attachments = [] |
| 48 | ): bool { |
| 49 | return email_send($html, $from, $to, $subject, $attachments); |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param array<string, mixed> $query |
| 55 | * @return array{output: string, contentType: string} |
| 56 | */ |
| 57 | private function runAction(string $actionName, string $body, array $query): array |
| 58 | { |
| 59 | $conx = new \ConxHelper(__FILE__); |
| 60 | if (!isset(self::$actionCache[$actionName])) { |
| 61 | $path = self::TICKET_ACTIONS_DIR.'/'.$actionName.'.php'; |
| 62 | self::$actionCache[$actionName] = require $path; |
| 63 | } |
| 64 | $action = self::$actionCache[$actionName]; |
| 65 | $logger = $this->loggerFactory->for(__FILE__); |
| 66 | try { |
| 67 | if ($actionName === 'Invoicepdf') { |
| 68 | return $action($body, $query, $conx, $logger, $this->sessionService, $this->html2PdfService); |
| 69 | } |
| 70 | if ($actionName === 'InvoiceSend') { |
| 71 | return $action( |
| 72 | $body, |
| 73 | $query, |
| 74 | $conx, |
| 75 | $logger, |
| 76 | $this->sessionService, |
| 77 | $this->emailSend, |
| 78 | 'staff' |
| 79 | ); |
| 80 | } |
| 81 | return $action($body, $query, $conx, $logger, $this->sessionService); |
| 82 | } finally { |
| 83 | $conx->release(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * POST /ticket/invoicepdf - render invoice PDF from HTML (staff session). |
| 89 | * |
| 90 | * @param array<string, mixed> $query |
| 91 | * @return array{output: string, contentType: string} |
| 92 | */ |
| 93 | public function invoicepdf(string $body, array $query): array |
| 94 | { |
| 95 | return $this->runAction('Invoicepdf', $body, $query); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * POST /ticket/invoicesend - email invoice PDF (staff session). |
| 100 | * |
| 101 | * @param array<string, mixed> $query |
| 102 | * @return array{output: string, contentType: string} |
| 103 | */ |
| 104 | public function invoicesend(string $body, array $query): array |
| 105 | { |
| 106 | return $this->runAction('InvoiceSend', $body, $query); |
| 107 | } |
| 108 | } |