Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.14% |
23 / 28 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CertificatePolicyService | |
82.14% |
23 / 28 |
|
71.43% |
5 / 7 |
13.96 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateFile | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
3.24 | |||
| getFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| deleteFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| updateOid | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getOid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCps | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Service; |
| 10 | |
| 11 | use OCA\Libresign\AppInfo\Application; |
| 12 | use OCA\Libresign\Exception\LibresignException; |
| 13 | use OCP\Files\IAppData; |
| 14 | use OCP\Files\NotFoundException; |
| 15 | use OCP\Files\SimpleFS\ISimpleFile; |
| 16 | use OCP\IAppConfig; |
| 17 | use OCP\IL10N; |
| 18 | use OCP\IURLGenerator; |
| 19 | use UnexpectedValueException; |
| 20 | |
| 21 | class CertificatePolicyService { |
| 22 | public function __construct( |
| 23 | private IAppData $appData, |
| 24 | private IURLGenerator $urlGenerator, |
| 25 | private IAppConfig $appConfig, |
| 26 | private IL10N $l10n, |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | public function updateFile(string $tmpFile): string { |
| 31 | $detectedMimeType = mime_content_type($tmpFile); |
| 32 | if ($detectedMimeType !== 'application/pdf') { |
| 33 | throw new UnexpectedValueException('Unsupported image type: ' . $detectedMimeType); |
| 34 | } |
| 35 | |
| 36 | $blob = file_get_contents($tmpFile); |
| 37 | $rootFolder = $this->appData->getFolder('/'); |
| 38 | try { |
| 39 | $rootFolder->newFile('certificate-policy.pdf', $blob); |
| 40 | } catch (NotFoundException) { |
| 41 | $file = $rootFolder->getFile('certificate-policy.pdf'); |
| 42 | $file->putContent($blob); |
| 43 | } |
| 44 | return $this->urlGenerator->linkToRouteAbsolute('libresign.CertificatePolicy.getCertificatePolicy'); |
| 45 | } |
| 46 | |
| 47 | public function getFile(): ISimpleFile { |
| 48 | return $this->appData->getFolder('/')->getFile('certificate-policy.pdf'); |
| 49 | } |
| 50 | |
| 51 | public function deleteFile(): void { |
| 52 | try { |
| 53 | $this->appData->getFolder('/')->getFile('certificate-policy.pdf')->delete(); |
| 54 | } catch (NotFoundException) { |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function updateOid(string $oid): string { |
| 59 | if (empty($oid)) { |
| 60 | $this->appConfig->deleteKey(Application::APP_ID, 'certificate_policies_oid'); |
| 61 | return ''; |
| 62 | } |
| 63 | $regex = '(0|1|2)(\.\d+)+'; |
| 64 | preg_match('/^' . $regex . '$/', $oid, $matches); |
| 65 | if (empty($matches)) { |
| 66 | // TRANSLATORS This message appears when an invalid Object |
| 67 | // Identifier (OID) is entered. It informs the admin that the input |
| 68 | // must follow a specific numeric pattern used in digital |
| 69 | // certificate policies. |
| 70 | throw new LibresignException($this->l10n->t('Invalid OID format. Expected pattern: %s', [$regex])); |
| 71 | } |
| 72 | $this->appConfig->setValueString(Application::APP_ID, 'certificate_policies_oid', $oid); |
| 73 | return $oid; |
| 74 | } |
| 75 | |
| 76 | public function getOid(): string { |
| 77 | return $this->appConfig->getValueString(Application::APP_ID, 'certificate_policies_oid', ''); |
| 78 | } |
| 79 | |
| 80 | public function getCps(): string { |
| 81 | try { |
| 82 | $this->getFile(); |
| 83 | } catch (NotFoundException) { |
| 84 | return ''; |
| 85 | } |
| 86 | return $this->urlGenerator->linkToRouteAbsolute('libresign.CertificatePolicy.getCertificatePolicy'); |
| 87 | } |
| 88 | } |