Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileService
95.24% covered (success)
95.24%
20 / 21
80.00% covered (warning)
80.00%
4 / 5
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRootCertificateByGeneration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPrivateKeyByGeneration
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadCertificateFileByGeneration
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
3.00
 readCertificateFile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Certificate;
10
11use OCA\Libresign\Enum\CertificateEngineType;
12use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
13use Psr\Log\LoggerInterface;
14
15class FileService {
16    public function __construct(
17        private CertificateEngineFactory $certificateEngineFactory,
18        private LoggerInterface $logger,
19    ) {
20    }
21
22    public function getRootCertificateByGeneration(string $instanceId, int $generation, CertificateEngineType $engineType): string {
23        return $this->loadCertificateFileByGeneration($instanceId, $generation, $engineType, 'ca.pem');
24    }
25
26    public function getPrivateKeyByGeneration(string $instanceId, int $generation, CertificateEngineType $engineType): string {
27        return $this->loadCertificateFileByGeneration($instanceId, $generation, $engineType, 'ca-key.pem');
28    }
29
30    private function loadCertificateFileByGeneration(string $instanceId, int $generation, CertificateEngineType $engineType, string $filename): string {
31        try {
32            $engine = $this->certificateEngineFactory->getEngine($engineType->getEngineName());
33            if (!method_exists($engine, 'getConfigPathByParams')) {
34                return '';
35            }
36
37            $configPath = $engine->getConfigPathByParams($instanceId, $generation);
38            return $this->readCertificateFile($configPath . DIRECTORY_SEPARATOR . $filename);
39        } catch (\Exception $e) {
40            $this->logger->debug('Failed to load certificate file', [
41                'instanceId' => $instanceId,
42                'generation' => $generation,
43                'engineType' => $engineType->value,
44                'filename' => $filename,
45                'error' => $e->getMessage(),
46            ]);
47            return '';
48        }
49    }
50
51    private function readCertificateFile(string $filePath): string {
52        if (!file_exists($filePath) || !is_readable($filePath)) {
53            return '';
54        }
55
56        $content = file_get_contents($filePath);
57        return $content !== false ? $content : '';
58    }
59}