Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CertificateEngineSetupCheck
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
11
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\SetupCheck;
10
11use OCA\Libresign\Exception\LibresignException;
12use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
13use OCA\Libresign\Helper\ConfigureCheckHelper;
14use OCP\IL10N;
15use OCP\SetupCheck\ISetupCheck;
16use OCP\SetupCheck\SetupResult;
17
18class CertificateEngineSetupCheck implements ISetupCheck {
19    private IL10N $l10n;
20    private CertificateEngineFactory $certificateEngineFactory;
21
22    public function __construct(
23        IL10N $l10n,
24        CertificateEngineFactory $certificateEngineFactory,
25    ) {
26        $this->l10n = $l10n;
27        $this->certificateEngineFactory = $certificateEngineFactory;
28    }
29
30    #[\Override]
31    public function getName(): string {
32        // TRANSLATORS Name of the Nextcloud administration overview check for the LibreSign certificate engine used to issue signing certificates.
33        return $this->l10n->t('Certificate engine');
34    }
35
36    #[\Override]
37    public function getCategory(): string {
38        return 'security';
39    }
40
41    #[\Override]
42    public function run(): SetupResult {
43        try {
44            $engine = $this->certificateEngineFactory->getEngine();
45            $checkResults = $engine->configureCheck();
46        } catch (LibresignException $e) {
47            $engineName = $this->certificateEngineFactory->getEngine()->getName() ?? 'unknown';
48            return SetupResult::error(
49                // TRANSLATORS Warning shown in Nextcloud administration overview when LibreSign has no certificate engine configured.
50                $this->l10n->t('Define the certificate engine to use'),
51                // TRANSLATORS Tip under a Nextcloud administration overview error. Instructs the administrator to configure the certificate engine with occ. %s is the engine name.
52                $this->l10n->t('Run occ libresign:configure:%s --help', [$engineName])
53            );
54        }
55
56        $hasError = false;
57        $hasWarning = false;
58        $messages = [];
59        $tips = [];
60
61        foreach ($checkResults as $result) {
62            if ($result instanceof ConfigureCheckHelper) {
63                $status = $result->getStatus();
64                $msg = $result->getMessage();
65                $tip = $result->getTip();
66
67                if ($status === 'error') {
68                    $hasError = true;
69                    $messages[] = "[ERROR] $msg";
70                } elseif ($status === 'warning') {
71                    $hasWarning = true;
72                    $messages[] = "[WARNING] $msg";
73                } else {
74                    $messages[] = $msg;
75                }
76
77                if (!empty($tip)) {
78                    $tips[] = $tip;
79                }
80            }
81        }
82
83        $tip = '';
84        if (!empty($tips)) {
85            $tip = implode("\n", array_unique($tips));
86        }
87
88        if ($hasError) {
89            return SetupResult::error(implode("\n", $messages), $tip);
90        } elseif ($hasWarning) {
91            return SetupResult::warning(implode("\n", $messages), $tip);
92        } else {
93            // TRANSLATORS Success message in Nextcloud administration overview when the LibreSign certificate engine is configured correctly.
94            return SetupResult::success(implode("\n", $messages) ?: $this->l10n->t('Certificate engine is configured correctly'));
95        }
96    }
97}