Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
17 / 51 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Fiscal action: download signed fiscal authorization PDF. |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | /** |
| 11 | * Action callable: return signed authorization PDF bytes. |
| 12 | * |
| 13 | * @param string $body Unused. |
| 14 | * @param array<string, mixed> $query Query params. |
| 15 | * @param \ConxHelper $conx Connection helper. |
| 16 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 17 | * @param \UppServices\SessionService $sessionService Session service. |
| 18 | * @return array{output: string, contentType: string, filename?: string} |
| 19 | */ |
| 20 | return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array { |
| 21 | $retval = \ApplicationError::Success; |
| 22 | $fileContents = ''; |
| 23 | $filename = ''; |
| 24 | |
| 25 | $device = $query['device'] ?? null; |
| 26 | if (success($retval) && !$device) { |
| 27 | $retval = \ApplicationError::Parameters; |
| 28 | $logger->error("No device provided on URL. Operation cancelled."); |
| 29 | } |
| 30 | |
| 31 | if (success($retval)) { |
| 32 | $session = $query['session'] ?? null; |
| 33 | if (!$session) { |
| 34 | $retval = \ApplicationError::Parameters; |
| 35 | $logger->error("No session provided on URL. Operation cancelled."); |
| 36 | } |
| 37 | else { |
| 38 | $userid = null; |
| 39 | $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | $place = null; |
| 44 | if (success($retval)) { |
| 45 | $place = $query['place'] ?? null; |
| 46 | if (!$place) { |
| 47 | $retval = \ApplicationError::Parameters; |
| 48 | $logger->error("Mandatory parameter 'place' not provided on URL. Operation cancelled."); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (success($retval)) { |
| 53 | $filename = rawurldecode((string) ($query['filename'] ?? '')); |
| 54 | if ($filename === '') { |
| 55 | $filename = str_pad((string) ((int) $place), 8, '0', STR_PAD_LEFT).'_autorizacion_firmada.pdf'; |
| 56 | } |
| 57 | |
| 58 | if (!preg_match('/^[A-Za-z0-9_\-\.]+$/', $filename)) { |
| 59 | $retval = \ApplicationError::Parameters; |
| 60 | $logger->error("Invalid filename requested in download."); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (success($retval)) { |
| 65 | $legalFolder = \AppConstants::LegalPath(); |
| 66 | if ($legalFolder === '' || !is_dir($legalFolder)) { |
| 67 | $retval = \ApplicationError::NotFound; |
| 68 | $logger->error("Legal folder was not found at '".$legalFolder."'."); |
| 69 | } |
| 70 | else { |
| 71 | $filepath = $legalFolder.'/'.$filename; |
| 72 | if (!file_exists($filepath)) { |
| 73 | $retval = \ApplicationError::NotFound; |
| 74 | $logger->error("Signed authorization PDF not found at '".$filepath."'."); |
| 75 | } |
| 76 | else { |
| 77 | $contents = file_get_contents($filepath); |
| 78 | if ($contents === false) { |
| 79 | $retval = \ApplicationError::Unknown; |
| 80 | $logger->error("Could not read signed authorization PDF '".$filepath."'."); |
| 81 | } |
| 82 | else { |
| 83 | $fileContents = $contents; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (!success($retval)) { |
| 90 | $payload = json_encode(['errorcode' => $retval]); |
| 91 | return ['output' => $payload, 'contentType' => 'application/json; charset=utf-8']; |
| 92 | } |
| 93 | |
| 94 | return [ |
| 95 | 'output' => $fileContents, |
| 96 | 'contentType' => 'application/pdf', |
| 97 | 'filename' => $filename |
| 98 | ]; |
| 99 | }; |
| 100 | ?> |