Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
VerifactuService
93.33% covered (success)
93.33%
14 / 15
66.67% covered (warning)
66.67%
2 / 3
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 async
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 audit
83.33% covered (success)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
1<?php
2
3declare(strict_types=1);
4
5namespace UppServices;
6
7require_once __DIR__.'/legacy/ddbb/conx.php';
8
9/**
10 * VeriFactu async and audit-retry actions.
11 * Delegates to action files in App/Services/verifactu/.
12 */
13class VerifactuService
14{
15    private const VERIFACTU_ACTIONS_DIR = __DIR__.'/verifactu';
16
17    private readonly SessionService $sessionService;
18    private readonly LoggerFactory $loggerFactory;
19
20    /** @var callable|null */
21    private static $asyncAction = null;
22
23    public function __construct(
24        SessionService $sessionService,
25        LoggerFactory $loggerFactory,
26    ) {
27        $this->sessionService = $sessionService;
28        $this->loggerFactory = $loggerFactory;
29    }
30
31    /**
32     * POST /verifactu/async.
33     *
34     * @param string $rawBody Raw request body (JSON).
35     * @return array{output: string, contentType: string}
36     */
37    public function async(string $rawBody): array
38    {
39        $conx = new \ConxHelper(__FILE__);
40        if (self::$asyncAction === null) {
41            self::$asyncAction = require_once self::VERIFACTU_ACTIONS_DIR.'/AsyncSend.php';
42        }
43        $action = self::$asyncAction;
44        $logger = $this->loggerFactory->for(__FILE__);
45        try {
46            return $action($rawBody, $conx, $logger);
47        } finally {
48            $conx->release();
49        }
50    }
51
52    /**
53     * GET /verifactu/audit — retry pending/failed VeriFactu rows for one audit period.
54     *
55     * @param array<string, string|int|float|null> $query
56     * @return array{errorcode: int, ticketsok: mixed, ticketsko: mixed}
57     */
58    public function audit(array $query): array
59    {
60        $conx = new \ConxHelper(__FILE__);
61        if (!function_exists('verifactu_audit_retry_run')) {
62            require_once self::VERIFACTU_ACTIONS_DIR.'/AuditRetry.php';
63        }
64        $logger = $this->loggerFactory->for(__FILE__);
65        try {
66            return \verifactu_audit_retry_run($query, $conx, $logger, $this->sessionService);
67        } finally {
68            $conx->release();
69        }
70    }
71}