Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SigningProgressService
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setInProgressStatus
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 revertInProgressStatus
100.00% covered (success)
100.00%
10 / 10
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;
10
11use OCA\Libresign\Db\File as FileEntity;
12use OCA\Libresign\Enum\FileStatus;
13use Psr\Log\LoggerInterface;
14
15/**
16 * Service to manage signing progress status
17 *
18 * Handles setting and reverting the SIGNING_IN_PROGRESS status
19 * for files during the signing process.
20 */
21class SigningProgressService {
22    public function __construct(
23        private FileStatusService $fileStatusService,
24        private LoggerInterface $logger,
25    ) {
26    }
27
28    /**
29     * Set SIGNING_IN_PROGRESS status for files before signing
30     *
31     * @param array $signRequests Sign requests with 'file' => FileEntity, 'signRequest' => SignRequestEntity
32     * @throws \Exception
33     */
34    public function setInProgressStatus(array $signRequests): void {
35        foreach ($signRequests as $signRequest) {
36            $file = $signRequest['file'];
37            $currentStatus = $file->getStatus();
38
39            if ($currentStatus !== FileStatus::SIGNING_IN_PROGRESS->value) {
40                $file->setStatus(FileStatus::SIGNING_IN_PROGRESS->value);
41                $this->fileStatusService->update($file);
42            }
43        }
44    }
45
46    /**
47     * Revert SIGNING_IN_PROGRESS status back to ABLE_TO_SIGN on error
48     *
49     * @param FileEntity[] $files Files to revert status
50     * @param \Exception|null $exception Optional exception that caused the revert
51     * @throws \Exception
52     */
53    public function revertInProgressStatus(array $files, ?\Exception $exception = null): void {
54        foreach ($files as $file) {
55            $currentStatus = $file->getStatus();
56
57            if ($currentStatus === FileStatus::SIGNING_IN_PROGRESS->value) {
58                $file->setStatus(FileStatus::ABLE_TO_SIGN->value);
59                $this->fileStatusService->update($file);
60            }
61        }
62
63        if ($exception !== null) {
64            $this->logger->error('Failed to complete signing: {error}', [
65                'error' => $exception->getMessage(),
66                'exception' => $exception,
67            ]);
68        }
69    }
70}