Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
FiscalService
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 runAction
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 authorization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 download
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 preview
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7require_once __DIR__ . '/legacy/ddbb/conx.php';
8
9/**
10 * Fiscal authorization actions: certificate validation, signed authorization generation and download.
11 * Delegates to action files in App/Services/fiscal/.
12 */
13class FiscalService
14{
15    private const FISCAL_ACTIONS_DIR = __DIR__.'/fiscal';
16
17    private readonly SessionService $sessionService;
18    private readonly LoggerFactory $loggerFactory;
19
20    /** @var array<string, callable> cache of loaded action callables */
21    private static array $actionCache = [];
22
23    public function __construct(
24        SessionService $sessionService,
25        LoggerFactory $loggerFactory,
26    ) {
27        $this->sessionService = $sessionService;
28        $this->loggerFactory = $loggerFactory;
29    }
30
31    /**
32     * Load and run a fiscal action. Creates ConxHelper; caller releases in finally.
33     *
34     * @param string $actionName Action file name (without .php).
35     * @param string $body Request body (for POST).
36     * @param array<string, mixed> $query Query params.
37     * @return array{output: string, contentType: string, filename?: string}
38     */
39    private function runAction(string $actionName, string $body, array $query): array
40    {
41        $conx = new \ConxHelper(__FILE__);
42        if (!isset(self::$actionCache[$actionName])) {
43            $path = self::FISCAL_ACTIONS_DIR.'/'.$actionName.'.php';
44            self::$actionCache[$actionName] = require_once $path;
45        }
46        $action = self::$actionCache[$actionName];
47        $logger = $this->loggerFactory->for(__FILE__);
48        try {
49            return $action($body, $query, $conx, $logger, $this->sessionService);
50        } finally {
51            $conx->release();
52        }
53    }
54
55    /**
56     * POST /fiscal/authorization - generate signed authorization PDF.
57     *
58     * @param array<string, mixed> $query Query params.
59     * @param array<string, mixed> $payload Multipart/form payload.
60     * @return array{output: string, contentType: string}
61     */
62    public function authorization(array $query, array $payload): array
63    {
64        return $this->runAction('Authorization', json_encode($payload), $query);
65    }
66
67    /**
68     * POST /fiscal/validate - validate certificate before signing.
69     *
70     * @param array<string, mixed> $query Query params.
71     * @param array<string, mixed> $payload Multipart/form payload.
72     * @return array{output: string, contentType: string}
73     */
74    public function validate(array $query, array $payload): array
75    {
76        return $this->runAction('Validate', json_encode($payload), $query);
77    }
78
79    /**
80     * GET /fiscal/download - download signed authorization PDF.
81     *
82     * @param array<string, mixed> $query Query params.
83     * @return array{output: string, contentType: string, filename?: string}
84     */
85    public function download(array $query): array
86    {
87        return $this->runAction('Download', '', $query);
88    }
89
90    /**
91     * POST /fiscal/preview - generate unsigned authorization PDF for review.
92     *
93     * @param array<string, mixed> $query Query params.
94     * @param array<string, mixed> $payload Request payload.
95     * @return array{output: string, contentType: string, filename?: string}
96     */
97    public function preview(array $query, array $payload): array
98    {
99        return $this->runAction('Preview', json_encode($payload), $query);
100    }
101}