• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rich-id / pdf-template-bundle / #20

11 Mar 2024 01:39PM UTC coverage: 0.0%. Remained the same
#20

push

Matthias Devlamynck
Update dependencies

0 of 179 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Domain/Pdf/AbstractPdf.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace RichId\PdfTemplateBundle\Domain\Pdf;
6

7
use HeadlessChromium\Page;
8
use Knp\Bundle\GaufretteBundle\FilesystemMap;
9
use RichId\PdfTemplateBundle\Domain\Constant;
10
use RichId\PdfTemplateBundle\Domain\Exception\MissingFilesystemException;
11
use RichId\PdfTemplateBundle\Domain\Exception\PdfNotFoundException;
12
use RichId\PdfTemplateBundle\Domain\Exception\PdfSkippedException;
13
use RichId\PdfTemplateBundle\Domain\Fetcher\PdfTemplateFetcher;
14
use RichId\PdfTemplateBundle\Domain\Internal\InternalPdfManager;
15
use RichId\PdfTemplateBundle\Domain\Model\PdfForcedTemplateSlugModelInterface;
16
use RichId\PdfTemplateBundle\Domain\Model\SaveablePdfModel;
17
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfDataTrait;
18
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfGeneratorTrait;
19
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfMergerTrait;
20
use RichId\PdfTemplateBundle\Domain\Pdf\Trait\PdfProtectionTrait;
21
use RichId\PdfTemplateBundle\Domain\Port\ConfigurationInterface;
22
use RichId\PdfTemplateBundle\Domain\Port\TemplatingInterface;
23
use RichId\PdfTemplateBundle\Domain\Port\TranslatorInterface;
24
use Symfony\Contracts\Service\Attribute\Required;
25

26
abstract class AbstractPdf
27
{
28
    use PdfDataTrait;
29
    use PdfGeneratorTrait;
30
    use PdfMergerTrait;
31
    use PdfProtectionTrait;
32

33
    public const TEMPLATES = [Constant::DEFAULT_TEMPLATE];
34

35
    protected const TRANSLATION_DOMAIN = 'pdf';
36
    protected const TEMPLATING_FOLDER = 'pdf';
37
    protected const MODIFICATION_ALLOWED = true;
38
    protected const OTHER_PAGES = [];
39
    protected const MAX_TIMEOUT = 10000;
40

41
    #[Required]
42
    public TemplatingInterface $templating;
43

44
    #[Required]
45
    public TranslatorInterface $translator;
46

47
    #[Required]
48
    public PdfTemplateFetcher $pdfTemplateFetcher;
49

50
    #[Required]
51
    public ConfigurationInterface $configuration;
52

53
    #[Required]
54
    public InternalPdfManager $internalPdfanager;
55

56
    #[Required]
57
    public FilesystemMap $filesystemMap;  /* @phpstan-ignore-line */
58

59
    abstract public function getPdfSlug(): string;
60

61
    protected function assertValidParameters(): void
62
    {
63
    }
×
64

65
    protected function updatePage(Page $page): void
66
    {
67
    }
×
68

69
    /** @return array<string, mixed> */
70
    protected function getPdfOptions(): array
71
    {
72
        return ['printBackground' => true];
×
73
    }
74

75
    protected function getSaveableModel(): ?SaveablePdfModel
76
    {
77
        return null;
×
78
    }
79

80
    public function getName(): string
81
    {
82
        return $this->translator->trans(
×
83
            \sprintf('%s.name', $this->getPdfSlug()),
×
84
            [],
×
85
            static::TRANSLATION_DOMAIN
×
86
        );
×
87
    }
88

89
    protected function getContent(): string
90
    {
91
        $data = $this->customBodyParameters();
×
92
        $data['data'] = $this->data;
×
93

94
        return $this->templating->render(
×
95
            \sprintf('%s/%s/%s.html.twig', static::TEMPLATING_FOLDER, $this->getPdfSlug(), $this->getTemplateSlug()),
×
96
            $data
×
97
        );
×
98
    }
99

100
    /** @return array<string, mixed> */
101
    protected function customBodyParameters(): array
102
    {
103
        return [];
×
104
    }
105

106
    /** @return array<string, callable> */
107
    protected function otherPagesDatas(): array
108
    {
109
        return [];
×
110
    }
111

112
    protected function skippedIf(): bool
113
    {
114
        return false;
×
115
    }
116

117
    final protected function generatePdf(): string
118
    {
119
        $pdf = $this->internalGeneratePdf($this->getContent(), static::MAX_TIMEOUT);
×
120
        $othersPages = $this->generateOtherPages();
×
121

122
        if (!empty($othersPages)) {
×
123
            $pdf = $this->mergePdfs(\array_merge([$pdf], $othersPages));
×
124
        }
125

126
        if (static::MODIFICATION_ALLOWED) {
×
127
            return $pdf;
×
128
        }
129

130
        return $this->internalProtectPdf($pdf);
×
131
    }
132

133
    /** @return string[] */
134
    final protected function generateOtherPages(): array
135
    {
136
        $othersPages = [];
×
137

138
        foreach (static::OTHER_PAGES as $otherPageSlug) {
×
139
            $pageService = $this->internalPdfanager->getCurrentPdfService($otherPageSlug);
×
140

141
            if ($pageService === null) {
×
142
                throw new PdfNotFoundException($otherPageSlug);
×
143
            }
144

145
            $data = $this->data;
×
146
            $customDataFn = $this->otherPagesDatas()[$otherPageSlug] ?? null;
×
147

148
            if (\is_callable($customDataFn)) {
×
149
                $data = $customDataFn();
×
150
            }
151
            $pageService->setData($data);
×
152
            $pageService->assertValidParameters();
×
153

154
            if ($pageService->skippedIf()) {
×
155
                continue;
×
156
            }
157

158
            $othersPages[] = $pageService->generatePdf();
×
159
        }
160

161
        return $othersPages;
×
162
    }
163

164
    final protected function getPdf(): string
165
    {
166
        $this->assertValidParameters();
×
167

168
        $seveableModel = $this->getSaveableModel();
×
169

170
        if ($seveableModel !== null && !$this->filesystemMap->has($seveableModel->getFilesystemName())) {
×
171
            throw new MissingFilesystemException($seveableModel->getFilesystemName());
×
172
        }
173

174
        $fs = $seveableModel !== null ? $this->filesystemMap->get($seveableModel->getFilesystemName()) : null;
×
175

176
        if ($seveableModel !== null && $fs !== null && $fs->has($seveableModel->getFileName()) && !$seveableModel->canForceNewGeneration()) {
×
177
            return $fs->get($seveableModel->getFileName())->getContent();
×
178
        }
179

180
        if ($this->skippedIf()) {
×
181
            throw new PdfSkippedException();
×
182
        }
183

184
        $pdf = $this->generatePdf();
×
185

186
        if ($seveableModel !== null && $fs !== null && $seveableModel->canSave()) {
×
187
            $fs->createFile($seveableModel->getFileName())->setContent($pdf);
×
188
        }
189

190
        return $pdf;
×
191
    }
192

193
    final public function supportTemplate(string $template): bool
194
    {
195
        return \in_array($template, static::TEMPLATES);
×
196
    }
197

198
    final protected function getTemplateSlug(): string
199
    {
200
        if ($this->data instanceof PdfForcedTemplateSlugModelInterface && $this->data->getForcedTemplateSlug() !== null) {
×
201
            $this->data->getForcedTemplateSlug();
×
202
        }
203

204
        return ($this->pdfTemplateFetcher)($this->getPdfSlug());
×
205
    }
206
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc