Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
AccountFileService
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
10
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
 getSignRequestByUuid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileByUuid
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getPdfByUuid
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 getFileByNodeId
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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\FileMapper;
12use OCA\Libresign\Db\SignRequest;
13use OCA\Libresign\Db\SignRequestMapper;
14use OCA\Libresign\Enum\FileStatus;
15use OCP\AppFramework\Db\DoesNotExistException;
16use OCP\Files\File;
17use OCP\Files\NotFoundException;
18
19class AccountFileService {
20    public function __construct(
21        private SignRequestMapper $signRequestMapper,
22        private FileMapper $fileMapper,
23        private FolderService $folderService,
24    ) {
25    }
26
27    public function getSignRequestByUuid(string $uuid): SignRequest {
28        return $this->signRequestMapper->getByUuid($uuid);
29    }
30
31    /** Resolve a sign-request UUID to its database file and readable node. */
32    public function getFileByUuid(string $uuid): array {
33        $signRequest = $this->getSignRequestByUuid($uuid);
34        $fileData = $this->fileMapper->getById($signRequest->getFileId());
35        $fileToSign = $this->folderService->getReadableNodeById($fileData->getUserId(), $fileData->getNodeId());
36        return [
37            'fileData' => $fileData,
38            'fileToSign' => $fileToSign instanceof File ? $fileToSign : null,
39        ];
40    }
41
42    /**
43     * Resolve a document UUID to its signed or original PDF node.
44     *
45     * @throws DoesNotExistException When the document or its file node cannot be found.
46     */
47    public function getPdfByUuid(string $uuid): File {
48        $fileData = $this->fileMapper->getByUuid($uuid);
49
50        if (in_array($fileData->getStatus(), [FileStatus::PARTIAL_SIGNED->value, FileStatus::SIGNED->value])) {
51            $nodeId = $fileData->getSignedNodeId();
52        } else {
53            $nodeId = $fileData->getNodeId();
54        }
55        if ($nodeId === null) {
56            throw new DoesNotExistException('Not found');
57        }
58
59        $userId = $this->fileMapper->getStorageUserIdByUuid($uuid);
60        $this->folderService->setUserId($userId);
61        try {
62            return $this->folderService->getFileByNodeId($nodeId);
63        } catch (NotFoundException) {
64            throw new DoesNotExistException('Not found');
65        }
66    }
67
68    /**
69     * @throws DoesNotExistException When the file node cannot be found.
70     */
71    public function getFileByNodeId(int $nodeId): File {
72        try {
73            return $this->folderService->getFileByNodeId($nodeId);
74        } catch (NotFoundException) {
75            throw new DoesNotExistException('Not found');
76        }
77    }
78}