Skip to content

Commit

Permalink
Introduce ForcedTemplateSlug logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Feb 16, 2024
1 parent bb9bfeb commit a9ab7b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Domain/Internal/InternalPdfManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ final class InternalPdfManager
#[Required]
public PdfTemplateFetcher $pdfTemplateFetcher;

public function getCurrentPdfService(string $slug): ?AbstractPdf
public function getCurrentPdfService(string $slug, ?string $forcedTemplateSlug = null): ?AbstractPdf
{
$services = $this->getAllPdfServicesFor($slug);
$template = ($this->pdfTemplateFetcher)($slug);
$template = $forcedTemplateSlug ?? ($this->pdfTemplateFetcher)($slug);

foreach ($services as $service) {
if ($service->supportTemplate($template)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Domain/Model/PdfForcedTemplateSlugModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace RichId\PdfTemplateBundle\Domain\Model;

interface PdfForcedTemplateSlugModelInterface extends PdfModelInterface
{
public function getForcedTemplateSlug(): ?string;

public function setForcedTemplateSlug(?string $forcedTemplateSlug): void;
}
5 changes: 5 additions & 0 deletions src/Domain/Pdf/AbstractPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RichId\PdfTemplateBundle\Domain\Exception\PdfSkippedException;
use RichId\PdfTemplateBundle\Domain\Fetcher\PdfTemplateFetcher;
use RichId\PdfTemplateBundle\Domain\Internal\InternalPdfManager;
use RichId\PdfTemplateBundle\Domain\Model\PdfForcedTemplateSlugModelInterface;
use RichId\PdfTemplateBundle\Domain\Model\SaveablePdfModel;
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfDataTrait;
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfGeneratorTrait;
Expand Down Expand Up @@ -196,6 +197,10 @@ final public function supportTemplate(string $template): bool

final protected function getTemplateSlug(): string
{
if ($this->data instanceof PdfForcedTemplateSlugModelInterface && $this->data->getForcedTemplateSlug() !== null) {
$this->data->getForcedTemplateSlug();
}

return ($this->pdfTemplateFetcher)($this->getPdfSlug());
}
}
9 changes: 7 additions & 2 deletions src/Domain/PdfManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use RichId\PdfTemplateBundle\Domain\Exception\PdfNotFoundException;
use RichId\PdfTemplateBundle\Domain\Internal\InternalPdfManager;
use RichId\PdfTemplateBundle\Domain\Model\PdfForcedTemplateSlugModelInterface;
use RichId\PdfTemplateBundle\Domain\Model\PdfModelInterface;
use Symfony\Contracts\Service\Attribute\Required;

Expand All @@ -14,9 +15,13 @@ final class PdfManager
#[Required]
public InternalPdfManager $internalPdfanager;

public function generatePdf(string $slug, ?PdfModelInterface $data = null): string
public function generatePdf(string $slug, ?PdfModelInterface $data = null, ?string $forcedTemplateSlug = null): string
{
$service = $this->internalPdfanager->getCurrentPdfService($slug);
if ($data instanceof PdfForcedTemplateSlugModelInterface) {
$data->setForcedTemplateSlug($forcedTemplateSlug);
}

$service = $this->internalPdfanager->getCurrentPdfService($slug, $forcedTemplateSlug);

if ($service === null) {
throw new PdfNotFoundException($slug);
Expand Down

0 comments on commit a9ab7b4

Please sign in to comment.