Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
14 / 18 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| |
77.78% |
14 / 18 |
|
33.33% |
1 / 3 |
8.70 | |
0.00% |
0 / 1 |
|
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| applyStamp | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| configureCommand | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
5.51 | |||
| 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\PdfTk; |
| 10 | |
| 11 | use OCA\Libresign\AppInfo\Application; |
| 12 | use OCA\Libresign\Exception\FooterStampUnavailableException; |
| 13 | use OCA\Libresign\Helper\JavaHelper; |
| 14 | use OCA\Libresign\Vendor\mikehaertl\pdftk\Command; |
| 15 | use OCA\Libresign\Vendor\mikehaertl\pdftk\Pdf as BasePdf; |
| 16 | use OCP\IAppConfig; |
| 17 | use OCP\IL10N; |
| 18 | use RuntimeException; |
| 19 | |
| 20 | class Pdf extends BasePdf { |
| 21 | private string $javaPath = ''; |
| 22 | private string $pdftkPath = ''; |
| 23 | |
| 24 | public function __construct( |
| 25 | private JavaHelper $javaHelper, |
| 26 | private IAppConfig $appConfig, |
| 27 | private IL10N $l10n, |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | public function applyStamp(string $input, string $stamp): string { |
| 32 | $this->configureCommand(); |
| 33 | |
| 34 | $this->addFile($input); |
| 35 | |
| 36 | $buffer = $this->multiStamp($stamp)->toString(); |
| 37 | |
| 38 | if (!is_string($buffer)) { |
| 39 | throw new RuntimeException('Failed to merge the PDF with the footer.'); |
| 40 | } |
| 41 | |
| 42 | return $buffer; |
| 43 | } |
| 44 | |
| 45 | protected function configureCommand(): void { |
| 46 | $this->javaPath = $this->javaHelper->getJavaPath(); |
| 47 | if ($this->javaPath === '') { |
| 48 | throw new FooterStampUnavailableException('Java path not set.'); |
| 49 | } |
| 50 | |
| 51 | $this->pdftkPath = $this->appConfig->getValueString(Application::APP_ID, 'pdftk_path'); |
| 52 | if ($this->pdftkPath === '') { |
| 53 | throw new FooterStampUnavailableException('PDFtk path not set.'); |
| 54 | } |
| 55 | |
| 56 | if (!file_exists($this->javaPath) || !file_exists($this->pdftkPath)) { |
| 57 | // TRANSLATORS Error while LibreSign tries to stamp the signed PDF footer with PDFtk: Java or PDFtk is not installed yet on the server. |
| 58 | throw new FooterStampUnavailableException($this->l10n->t('The admin hasn\'t set up LibreSign yet, please wait.')); |
| 59 | } |
| 60 | |
| 61 | $cmd = sprintf('%s -jar %s', escapeshellcmd($this->javaPath), escapeshellarg($this->pdftkPath)); |
| 62 | |
| 63 | $this->_command = new Command(); |
| 64 | $this->_command->setCommand($cmd); |
| 65 | } |
| 66 | } |