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

LibreSign / libresign / 19936817637

04 Dec 2025 04:50PM UTC coverage: 41.559%. First build
19936817637

Pull #5991

github

web-flow
Merge 6c27ee100 into 39934aacc
Pull Request #5991: refactor: footer handler decoupling

59 of 66 new or added lines in 3 files covered. (89.39%)

5172 of 12445 relevant lines covered (41.56%)

4.18 hits per line

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

86.14
/lib/Handler/FooterHandler.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Handler;
10

11
use OCA\Libresign\AppInfo\Application;
12
use OCA\Libresign\Db\File as FileEntity;
13
use OCA\Libresign\Exception\LibresignException;
14
use OCA\Libresign\Service\PdfParserService;
15
use OCA\Libresign\Vendor\Endroid\QrCode\Color\Color;
16
use OCA\Libresign\Vendor\Endroid\QrCode\Encoding\Encoding;
17
use OCA\Libresign\Vendor\Endroid\QrCode\ErrorCorrectionLevel;
18
use OCA\Libresign\Vendor\Endroid\QrCode\QrCode;
19
use OCA\Libresign\Vendor\Endroid\QrCode\RoundBlockSizeMode;
20
use OCA\Libresign\Vendor\Endroid\QrCode\Writer\PngWriter;
21
use OCA\Libresign\Vendor\Mpdf\Mpdf;
22
use OCA\Libresign\Vendor\Twig\Environment;
23
use OCA\Libresign\Vendor\Twig\Error\SyntaxError;
24
use OCA\Libresign\Vendor\Twig\Loader\FilesystemLoader;
25
use OCP\Files\File;
26
use OCP\IAppConfig;
27
use OCP\IL10N;
28
use OCP\ITempManager;
29
use OCP\IURLGenerator;
30
use OCP\L10N\IFactory;
31

32
class FooterHandler {
33
        private QrCode $qrCode;
34
        private const MIN_QRCODE_SIZE = 100;
35
        private const POINT_TO_MILIMETER = 0.3527777778;
36

37
        private TemplateVariables $templateVars;
38

39
        public function __construct(
40
                private IAppConfig $appConfig,
41
                private PdfParserService $pdfParserService,
42
                private IURLGenerator $urlGenerator,
43
                private IL10N $l10n,
44
                private IFactory $l10nFactory,
45
                private ITempManager $tempManager,
46
        ) {
47
                $this->templateVars = new TemplateVariables();
55✔
48
        }
49

50
        public function getFooter(array $dimensions): string {
51
                $add_footer = (bool)$this->appConfig->getValueBool(Application::APP_ID, 'add_footer', true);
8✔
52
                if (!$add_footer) {
8✔
53
                        return '';
2✔
54
                }
55

56
                $htmlFooter = $this->getRenderedHtmlFooter();
6✔
57
                foreach ($dimensions as $dimension) {
6✔
58
                        if (!isset($pdf)) {
6✔
59
                                $pdf = new Mpdf([
6✔
60
                                        'tempDir' => $this->tempManager->getTempBaseDir(),
6✔
61
                                        'orientation' => 'P',
6✔
62
                                        'margin_left' => 0,
6✔
63
                                        'margin_right' => 0,
6✔
64
                                        'margin_top' => 0,
6✔
65
                                        'margin_bottom' => 0,
6✔
66
                                        'margin_header' => 0,
6✔
67
                                        'margin_footer' => 0,
6✔
68
                                        'format' => [
6✔
69
                                                $dimension['w'] * self::POINT_TO_MILIMETER,
6✔
70
                                                $dimension['h'] * self::POINT_TO_MILIMETER,
6✔
71
                                        ],
6✔
72
                                ]);
6✔
73
                                $pdf->SetDirectionality($this->templateVars->getDirection());
6✔
74
                        }
75
                        $pdf->AddPage(
6✔
76
                                orientation: 'P',
6✔
77
                                newformat: [
6✔
78
                                        $dimension['w'] * self::POINT_TO_MILIMETER,
6✔
79
                                        $dimension['h'] * self::POINT_TO_MILIMETER,
6✔
80
                                ],
6✔
81
                        );
6✔
82

83
                        $pdf->SetHTMLFooter($htmlFooter);
6✔
84
                }
85

86
                return $pdf->Output('', 'S');
6✔
87
        }
88

89
        public function getMetadata(File $file, FileEntity $fileEntity): array {
90
                $metadata = $fileEntity->getMetadata();
×
91
                if (!is_array($metadata) || !isset($metadata['d'])) {
×
92
                        $metadata = $this->pdfParserService
×
93
                                ->setFile($file)
×
94
                                ->getPageDimensions();
×
95
                }
96
                return $metadata;
×
97
        }
98

99
        private function getRenderedHtmlFooter(): string {
100
                try {
101
                        $twigEnvironment = new Environment(
6✔
102
                                new FilesystemLoader(),
6✔
103
                        );
6✔
104
                        return $twigEnvironment
6✔
105
                                ->createTemplate($this->getTemplate())
6✔
106
                                ->render($this->prepareTemplateVars());
6✔
107
                } catch (SyntaxError $e) {
×
108
                        throw new LibresignException($e->getMessage());
×
109
                }
110
        }
111

112
        public function setTemplateVar(string $name, mixed $value): self {
113
                $this->templateVars->merge([$name => $value]);
7✔
114
                return $this;
7✔
115
        }
116

117
        private function prepareTemplateVars(): array {
118
                if (!$this->templateVars->getSignedBy()) {
6✔
119
                        $this->templateVars->setSignedBy(
6✔
120
                                $this->appConfig->getValueString(Application::APP_ID, 'footer_signed_by', $this->l10n->t('Digitally signed by LibreSign.'))
6✔
121
                        );
6✔
122
                }
123

124
                if (!$this->templateVars->getDirection()) {
6✔
125
                        $this->templateVars->setDirection(
6✔
126
                                $this->l10nFactory->getLanguageDirection($this->l10n->getLanguageCode())
6✔
127
                        );
6✔
128
                }
129

130
                if (!$this->templateVars->getLinkToSite()) {
6✔
131
                        $this->templateVars->setLinkToSite(
6✔
132
                                $this->appConfig->getValueString(Application::APP_ID, 'footer_link_to_site', 'https://libresign.coop')
6✔
133
                        );
6✔
134
                }
135

136
                if (!$this->templateVars->getValidationSite() && $this->templateVars->getUuid()) {
6✔
137
                        $validationSite = $this->appConfig->getValueString(Application::APP_ID, 'validation_site');
4✔
138
                        if ($validationSite) {
4✔
139
                                $this->templateVars->setValidationSite(
4✔
140
                                        rtrim($validationSite, '/') . '/' . $this->templateVars->getUuid()
4✔
141
                                );
4✔
142
                        } else {
NEW
143
                                $this->templateVars->setValidationSite(
×
NEW
144
                                        $this->urlGenerator->linkToRouteAbsolute('libresign.page.validationFileWithShortUrl', [
×
NEW
145
                                                'uuid' => $this->templateVars->getUuid(),
×
NEW
146
                                        ])
×
NEW
147
                                );
×
148
                        }
149
                }
150

151
                if (!$this->templateVars->getValidateIn()) {
6✔
152
                        $validateIn = $this->appConfig->getValueString(Application::APP_ID, 'footer_validate_in', 'Validate in %s.');
6✔
153
                        if ($validateIn === 'Validate in %s.') {
6✔
154
                                $this->templateVars->setValidateIn($this->l10n->t('Validate in %s.', ['%s']));
2✔
155
                        } else {
156
                                $this->templateVars->setValidateIn($validateIn);
4✔
157
                        }
158
                }
159

160
                if ($this->appConfig->getValueBool(Application::APP_ID, 'write_qrcode_on_footer', true) && $this->templateVars->getValidationSite()) {
6✔
161
                        $this->templateVars->setQrcode($this->getQrCodeImageBase64($this->templateVars->getValidationSite()));
2✔
162
                }
163

164
                $vars = $this->templateVars->toArray();
6✔
165
                foreach ($vars as $key => $value) {
6✔
166
                        if (is_string($value)) {
6✔
167
                                $vars[$key] = htmlentities($value, ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML401);
6✔
168
                        }
169
                }
170

171
                return $vars;
6✔
172
        }
173

174
        private function getTemplate(): string {
175
                $footerTemplate = $this->appConfig->getValueString(Application::APP_ID, 'footer_template', '');
6✔
176
                if ($footerTemplate) {
6✔
177
                        return $footerTemplate;
6✔
178
                }
179
                return (string)file_get_contents(__DIR__ . '/Templates/footer.twig');
×
180
        }
181

182
        private function getQrCodeImageBase64(string $text): string {
183
                $this->qrCode = new QrCode(
2✔
184
                        data: $text,
2✔
185
                        encoding: new Encoding('UTF-8'),
2✔
186
                        errorCorrectionLevel: ErrorCorrectionLevel::Low,
2✔
187
                        size: self::MIN_QRCODE_SIZE,
2✔
188
                        margin: 4,
2✔
189
                        roundBlockSizeMode: RoundBlockSizeMode::Margin,
2✔
190
                        foregroundColor: new Color(0, 0, 0),
2✔
191
                        backgroundColor: new Color(255, 255, 255)
2✔
192
                );
2✔
193
                $writer = new PngWriter();
2✔
194
                $result = $writer->write($this->qrCode);
2✔
195
                $qrcode = base64_encode($result->getString());
2✔
196

197
                $this->templateVars->setQrcodeSize($this->qrCode->getSize() + $this->qrCode->getMargin() * 2);
2✔
198

199
                return $qrcode;
2✔
200
        }
201
}
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

© 2026 Coveralls, Inc