Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
28 / 30
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
HtmxPdfRequestHandler
96.55% covered (success)
96.55%
28 / 29
83.33% covered (warning)
83.33%
5 / 6
19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
 handleSignature
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 handleQueue
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 handleRecord
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 matchRecordModalRoutes
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Bootstrap\Configurator\Handler;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Pdf\Presentation\Htmx\PdfHtmxController;
12use App\Modules\Pdf\Presentation\Htmx\PdfQueueHtmxController;
13use App\Modules\Pdf\Presentation\Htmx\PdfSignatureHtmxController;
14use Nyholm\Psr7\Factory\Psr17Factory;
15use Psr\Http\Message\ResponseInterface;
16use Psr\Http\Message\ServerRequestInterface;
17use Psr\Http\Server\RequestHandlerInterface;
18
19/**
20 * PSR-15 Request Handler for HTMX PDF subsystem routes.
21 *
22 * @package App\Core\Bootstrap\Configurator\Handler
23 */
24final readonly class HtmxPdfRequestHandler implements RequestHandlerInterface
25{
26    public function __construct(
27        private PdfHtmxController $controller,
28        private ?PdfSignatureHtmxController $sigCtrl,
29        private ?PdfQueueHtmxController $qCtrl,
30        private string $path,
31        private Psr17Factory $psr17Factory
32    ) {
33    }
34
35    public function handle(ServerRequestInterface $request): ResponseInterface
36    {
37        return match (true) {
38            str_starts_with($this->path, '/htmx/pdf/signature-') => $this->handleSignature($request),
39            str_starts_with($this->path, '/htmx/pdf/queue/')     => $this->handleQueue($request),
40            default                                              => $this->handleRecord($request),
41        };
42    }
43
44    private function handleSignature(ServerRequestInterface $request): ResponseInterface
45    {
46        if ($this->path === '/htmx/pdf/signature-modal') {
47            return $this->sigCtrl?->modalSignature($request)
48                ?? $this->psr17Factory->createResponse(404);
49        }
50        if ($this->path === '/htmx/pdf/signature-save' && $request->getMethod() === 'POST') {
51            return $this->sigCtrl?->saveSignature($request)
52                ?? $this->psr17Factory->createResponse(404);
53        }
54
55        return $this->psr17Factory->createResponse(404);
56    }
57
58    private function handleQueue(ServerRequestInterface $request): ResponseInterface
59    {
60        if ($this->path === '/htmx/pdf/queue/create' && $request->getMethod() === 'POST') {
61            return $this->qCtrl?->createQueue($request)
62                ?? $this->psr17Factory->createResponse(404);
63        }
64        if (preg_match('#^/htmx/pdf/queue/status/(\d+)$#', $this->path, $m)) {
65            return $this->qCtrl?->status((int) $m[1])
66                ?? $this->psr17Factory->createResponse(404);
67        }
68
69        return $this->psr17Factory->createResponse(404);
70    }
71
72    private function handleRecord(ServerRequestInterface $request): ResponseInterface
73    {
74        $response = $this->matchRecordModalRoutes($request);
75        if ($response !== null) {
76            return $response;
77        }
78
79        return preg_match('#^/htmx/pdf/preview/(\d+)/(\d+)$#', $this->path, $m)
80            ? $this->controller->preview($request, (int) $m[1], (int) $m[2])
81            : $this->psr17Factory->createResponse(404);
82    }
83
84    private function matchRecordModalRoutes(ServerRequestInterface $request): ?ResponseInterface
85    {
86        if (preg_match('#^/htmx/pdf/([a-zA-Z0-9_-]+)/print-modal/(\d+)$#', $this->path, $m)) {
87            return $this->controller->modalPrintRecord($m[1], (int) $m[2]);
88        }
89        if (preg_match('#^/htmx/pdf/([a-zA-Z0-9_-]+)/bulk-modal$#', $this->path, $m)) {
90            return $this->controller->modalBulkPdf($request, $m[1]);
91        }
92
93        return null;
94    }
95}