Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RefreshGeneratedCrlCache
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 run
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
 getCurrentRefreshDate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
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\BackgroundJob;
10
11use OCA\Libresign\AppInfo\Application;
12use OCA\Libresign\Service\Crl\CrlService;
13use OCP\AppFramework\Utility\ITimeFactory;
14use OCP\BackgroundJob\IJob;
15use OCP\BackgroundJob\TimedJob;
16use OCP\IAppConfig;
17use Psr\Log\LoggerInterface;
18
19class RefreshGeneratedCrlCache extends TimedJob {
20    private const LAST_REFRESH_DATE_KEY = 'crl_generated_cache_last_refresh_date';
21
22    public function __construct(
23        ITimeFactory $time,
24        private CrlService $crlService,
25        private IAppConfig $appConfig,
26        private LoggerInterface $logger,
27    ) {
28        parent::__construct($time);
29
30        // Check frequently enough to refresh shortly after midnight without
31        // coupling the actual refresh time to the app installation time.
32        $this->setInterval(60 * 15);
33        $this->setTimeSensitivity(IJob::TIME_SENSITIVE);
34    }
35
36    /**
37     * @inheritDoc
38     */
39    #[\Override]
40    public function run($argument): void {
41        $currentRefreshDate = $this->getCurrentRefreshDate();
42        $lastRefreshDate = $this->appConfig->getValueString(
43            Application::APP_ID,
44            self::LAST_REFRESH_DATE_KEY,
45            ''
46        );
47
48        if ($lastRefreshDate === $currentRefreshDate) {
49            return;
50        }
51
52        try {
53            $this->crlService->refreshGeneratedCrlCache();
54            $this->appConfig->setValueString(
55                Application::APP_ID,
56                self::LAST_REFRESH_DATE_KEY,
57                $currentRefreshDate
58            );
59        } catch (\Throwable $exception) {
60            $this->logger->error('Failed to refresh daily generated CRL cache: {message}', [
61                'message' => $exception->getMessage(),
62                'exception' => $exception,
63            ]);
64        }
65    }
66
67    private function getCurrentRefreshDate(): string {
68        return (new \DateTimeImmutable('@' . $this->time->getTime()))
69            ->setTimezone(new \DateTimeZone(date_default_timezone_get()))
70            ->format('Y-m-d');
71    }
72}