extract pdf use case

This commit is contained in:
Yisroel Baum 2026-06-26 08:16:15 +03:00
parent b1bee8a16e
commit bc7937d2b3
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
7 changed files with 141 additions and 26 deletions

View file

@ -0,0 +1,53 @@
<?php
namespace App\Element\UseCases\GetElementPdf;
use App\Exceptions\NotFoundException;
use App\Shared\Files\StoredFileReader;
class GetElementPdf
{
private const PDF_FOLDERS = ['short', 'long'];
private const PDF_MIME_TYPE = 'application/pdf';
public function __construct(private StoredFileReader $storedFileReader)
{
}
/**
* @throws NotFoundException
*/
public function execute(GetElementPdfRequest $request): GetElementPdfResult
{
if (!$this->isAllowedPdfFolder($request->folder)) {
throw new NotFoundException('PDF not found');
}
if (!$this->isValidPdfFileName($request->fileName)) {
throw new NotFoundException('PDF not found');
}
$path = "element-pdfs/$request->folder/$request->fileName";
$contents = $this->storedFileReader->read($path);
if ($contents === null) {
throw new NotFoundException('PDF not found');
}
return new GetElementPdfResult(
contents: $contents,
fileName: $request->fileName,
mimeType: self::PDF_MIME_TYPE,
);
}
private function isAllowedPdfFolder(string $folder): bool
{
return in_array($folder, self::PDF_FOLDERS, true);
}
private function isValidPdfFileName(string $fileName): bool
{
return preg_match('/^[A-Za-z0-9._-]+\.pdf$/', $fileName) === 1;
}
}