handle nullable pdf params

This commit is contained in:
Yisroel Baum 2026-06-26 10:09:50 +03:00
parent 9126b0e968
commit 8c8ee4c050
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 18 additions and 4 deletions

View file

@ -69,8 +69,8 @@ class ElementController
} }
public function showPdf( public function showPdf(
string $folder, ?string $folder,
string $fileName, ?string $fileName,
): Response|JsonResponse { ): Response|JsonResponse {
try { try {
$result = $this->getElementPdf->execute( $result = $this->getElementPdf->execute(
@ -79,6 +79,10 @@ class ElementController
fileName: $fileName, fileName: $fileName,
) )
); );
} catch (BadRequestException $exception) {
return new JsonResponse([
'error' => $exception->getMessage(),
], 400);
} catch (NotFoundException $exception) { } catch (NotFoundException $exception) {
return new JsonResponse([ return new JsonResponse([
'error' => $exception->getMessage(), 'error' => $exception->getMessage(),

View file

@ -2,6 +2,7 @@
namespace App\Element\UseCases\GetElementPdf; namespace App\Element\UseCases\GetElementPdf;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException; use App\Exceptions\NotFoundException;
use App\Shared\Files\Filesystem; use App\Shared\Files\Filesystem;
@ -16,10 +17,19 @@ class GetElementPdf
} }
/** /**
* @throws BadRequestException
* @throws NotFoundException * @throws NotFoundException
*/ */
public function execute(GetElementPdfRequest $request): GetElementPdfResult public function execute(GetElementPdfRequest $request): GetElementPdfResult
{ {
if ($request->folder === null) {
throw new BadRequestException('folder is required');
}
if ($request->fileName === null) {
throw new BadRequestException('fileName is required');
}
if (!$this->isAllowedPdfFolder($request->folder)) { if (!$this->isAllowedPdfFolder($request->folder)) {
throw new NotFoundException('PDF not found'); throw new NotFoundException('PDF not found');
} }

View file

@ -5,8 +5,8 @@ namespace App\Element\UseCases\GetElementPdf;
class GetElementPdfRequest class GetElementPdfRequest
{ {
public function __construct( public function __construct(
public string $folder, public ?string $folder,
public string $fileName, public ?string $fileName,
) { ) {
} }
} }