Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PdfWebRequestHandler
94.12% covered (success)
94.12%
16 / 17
75.00% covered (warning)
75.00%
3 / 4
14.04
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
3
 handleDownload
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 handleBuilder
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
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\Web\PdfWebController;
12use Nyholm\Psr7\Factory\Psr17Factory;
13use Psr\Http\Message\ResponseInterface;
14use Psr\Http\Message\ServerRequestInterface;
15use Psr\Http\Server\RequestHandlerInterface;
16
17/**
18 * PSR-15 Request Handler for Web PDF subsystem routes.
19 *
20 * @package App\Core\Bootstrap\Configurator\Handler
21 */
22final readonly class PdfWebRequestHandler implements RequestHandlerInterface
23{
24    public function __construct(
25        private PdfWebController $controller,
26        private string $path,
27        private Psr17Factory $psr17Factory
28    ) {
29    }
30
31    public function handle(ServerRequestInterface $request): ResponseInterface
32    {
33        if (str_starts_with($this->path, '/pdf/download/') || $this->path === '/pdf/bulk-download') {
34            return $this->handleDownload($request);
35        }
36
37        return $this->handleBuilder($request);
38    }
39
40    private function handleDownload(ServerRequestInterface $request): ResponseInterface
41    {
42        if (preg_match('#^/pdf/download/(\d+)/(\d+)$#', $this->path, $m)) {
43            return $this->controller->download($request, (int) $m[1], (int) $m[2]);
44        }
45        if ($this->path === '/pdf/bulk-download' && $request->getMethod() === 'POST') {
46            return $this->controller->bulkDownload($request);
47        }
48
49        return $this->psr17Factory->createResponse(404);
50    }
51
52    private function handleBuilder(ServerRequestInterface $request): ResponseInterface
53    {
54        if (preg_match('#^/pdf/(?:builder/(\d+)|templates/builder/(\d+))$#', $this->path, $m)) {
55            $id = (int)($m[1] !== '' ? $m[1] : $m[2]);
56            return $this->controller->builder($id);
57        }
58        if (preg_match('#^/pdf/(?:templates/save-blocks/(\d+)|save-blocks/(\d+))$#', $this->path, $m)
59            && $request->getMethod() === 'POST'
60        ) {
61            $id = (int)($m[1] !== '' ? $m[1] : $m[2]);
62            return $this->controller->saveBlocks($request, $id);
63        }
64
65        return $this->psr17Factory->createResponse(404);
66    }
67}