feat: dummy replacement pdf upload for environments other than prod
This commit is contained in:
@@ -4,10 +4,20 @@ namespace App\Service\Pdf;
|
||||
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\Upload;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class InvoiceApprover extends AbstractPdfRenderer
|
||||
{
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
array $options,
|
||||
private readonly UploadHandler $uploadHandler,
|
||||
) {
|
||||
parent::__construct($translator, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
@@ -16,24 +26,23 @@ class InvoiceApprover extends AbstractPdfRenderer
|
||||
$teamer = $disposition->getTeamer();
|
||||
$assignment = $disposition->getAssignment();
|
||||
$invoice = $disposition->getDocumentByType(Upload::TYPE_INVOICE);
|
||||
$originalFilename = $invoice->getFilename();
|
||||
|
||||
if ('application/pdf' !== $invoice->getMimeType()) {
|
||||
throw new \InvalidArgumentException('Expected PDF but got '.$invoice->getMimeType());
|
||||
}
|
||||
|
||||
// Load template
|
||||
$folder = substr($originalFilename, 0, 1);
|
||||
$filepath = sprintf('%s/uploads/invoice/%s/%s', $this->config['project_dir'], $folder, $originalFilename);
|
||||
if (false === file_exists($filepath)) {
|
||||
throw new \InvalidArgumentException('Uploaded document not found at '.$filepath);
|
||||
$sourcePath = $this->uploadHandler->getResolvedUploadFilepath($invoice);
|
||||
$targetPath = $this->uploadHandler->getUploadFilepath($invoice);
|
||||
|
||||
if (false === file_exists($sourcePath)) {
|
||||
throw new \InvalidArgumentException('Uploaded document not found at '.$sourcePath);
|
||||
}
|
||||
|
||||
$pdf = new Pdf();
|
||||
|
||||
$pdf->SetAuthor($teamer->getFullName(), true);
|
||||
$pdf->SetCreator($teamer->getFullName(), true);
|
||||
$pdf->appendPdf($filepath);
|
||||
$pdf->appendPdf($sourcePath);
|
||||
$pdf->setPage(1);
|
||||
|
||||
// Cost unit job profile
|
||||
@@ -53,9 +62,16 @@ class InvoiceApprover extends AbstractPdfRenderer
|
||||
$pdf->SetTextColor(0, 128, 0);
|
||||
$pdf->Text(20, 30, $pdf->encodeString('geprüft: '.$invoice->getApprovedBy()));
|
||||
|
||||
$this->createBackup($filepath);
|
||||
$filesystem = new Filesystem();
|
||||
$targetDir = \dirname($targetPath);
|
||||
|
||||
$pdf->Output('F', $filepath);
|
||||
if (false === $filesystem->exists($targetDir)) {
|
||||
$filesystem->mkdir($targetDir);
|
||||
}
|
||||
|
||||
$this->createBackup($targetPath);
|
||||
|
||||
$pdf->Output('F', $targetPath);
|
||||
|
||||
return $pdf;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ class UploadHandler
|
||||
private readonly Sanitizer $sanitizer,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly string $projectDir,
|
||||
private readonly string $environment,
|
||||
private readonly string $uploadReplacementFile,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -143,6 +145,27 @@ class UploadHandler
|
||||
return true === $absolute ? sprintf('%s/%s', $this->projectDir, $relativePath) : $relativePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the upload file path, substituting a replacement file in non-production environments
|
||||
* when configured. Use this for read-only access (downloads, email attachments).
|
||||
*/
|
||||
public function getResolvedUploadFilepath(Upload $upload): string
|
||||
{
|
||||
$originalPath = $this->getUploadFilepath($upload);
|
||||
|
||||
if ('' === $this->uploadReplacementFile || 'prod' === $this->environment) {
|
||||
return $originalPath;
|
||||
}
|
||||
|
||||
$replacementPath = sprintf('%s/%s', $this->projectDir, $this->uploadReplacementFile);
|
||||
|
||||
if (false === file_exists($replacementPath)) {
|
||||
return $originalPath;
|
||||
}
|
||||
|
||||
return $replacementPath;
|
||||
}
|
||||
|
||||
public function getTempUploadFilepath(UploadDto $uploadDto, string $uploadSessionId): string
|
||||
{
|
||||
$tempUploadDir = $this->getTempUploadDir($uploadSessionId);
|
||||
|
||||
Reference in New Issue
Block a user