Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ReportsService | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| runAction | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| complete | |
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 | |
| 9 | /** |
| 10 | * Reports-related actions. |
| 11 | * Delegates to action files in App/Services/reports/. Each action receives ConxHelper and $logger. |
| 12 | */ |
| 13 | class ReportsService |
| 14 | { |
| 15 | private const REPORTS_ACTIONS_DIR = __DIR__.'/reports'; |
| 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 reports 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} |
| 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::REPORTS_ACTIONS_DIR.'/'.$actionName.'.php'; |
| 44 | self::$actionCache[$actionName] = require $path; |
| 45 | } |
| 46 | $action = self::$actionCache[$actionName]; |
| 47 | $logger = $this->loggerFactory->for(__FILE__); |
| 48 | |
| 49 | try { |
| 50 | return $action($body, $query, $conx, $logger, $this->sessionService); |
| 51 | } finally { |
| 52 | $conx->release(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** POST /reports/complete - load partial graph for table+objid items */ |
| 57 | public function complete(string $body, array $query): array |
| 58 | { |
| 59 | return $this->runAction('Complete', $body, $query); |
| 60 | } |
| 61 | } |