• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 20882165631

10 Jan 2026 05:56PM UTC coverage: 44.646%. First build
20882165631

Pull #6433

github

web-flow
Merge eead2e4b3 into ecd36974e
Pull Request #6433: refactor: move all constants to FileStatus enum

31 of 56 new or added lines in 16 files covered. (55.36%)

6742 of 15101 relevant lines covered (44.65%)

5.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/Service/EnvelopeService.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Service;
10

11
use DateTime;
12
use OCA\Libresign\AppInfo\Application;
13
use OCA\Libresign\Db\File as FileEntity;
14
use OCA\Libresign\Db\FileMapper;
15
use OCA\Libresign\Enum\FileStatus;
16
use OCA\Libresign\Enum\NodeType;
17
use OCA\Libresign\Exception\LibresignException;
18
use OCP\AppFramework\Db\DoesNotExistException;
19
use OCP\IAppConfig;
20
use OCP\IL10N;
21
use Sabre\DAV\UUIDUtil;
22

23
class EnvelopeService {
24
        public function __construct(
25
                protected FileMapper $fileMapper,
26
                protected IL10N $l10n,
27
                protected IAppConfig $appConfig,
28
                protected FolderService $folderService,
29
        ) {
30
        }
×
31

32
        public function isEnabled(): bool {
33
                return $this->appConfig->getValueBool(Application::APP_ID, 'envelope_enabled', true);
×
34
        }
35

36
        /**
37
         * @throws LibresignException
38
         */
39
        public function validateEnvelopeConstraints(int $fileCount): void {
40
                if (!$this->isEnabled()) {
×
41
                        throw new LibresignException($this->l10n->t('Envelope feature is disabled'));
×
42
                }
43

44
                $maxFiles = $this->getMaxFilesPerEnvelope();
×
45
                if ($fileCount > $maxFiles) {
×
46
                        throw new LibresignException(
×
47
                                $this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles])
×
48
                        );
×
49
                }
50
        }
51

52
        public function createEnvelope(
53
                string $name,
54
                string $userId,
55
                int $filesCount = 0,
56
                ?string $path = null,
57
        ): FileEntity {
58
                $this->folderService->setUserId($userId);
×
59

60
                $uuid = UUIDUtil::getUUID();
×
61
                if ($path) {
×
62
                        $envelopeFolder = $this->folderService->getOrCreateFolderByAbsolutePath($path);
×
63
                } else {
64
                        $parentFolder = $this->folderService->getFolder();
×
65
                        $folderName = $name . '_' . $uuid;
×
66
                        $envelopeFolder = $parentFolder->newFolder($folderName);
×
67
                }
68

69
                $envelope = new FileEntity();
×
70
                $envelope->setNodeId($envelopeFolder->getId());
×
71
                $envelope->setNodeTypeEnum(NodeType::ENVELOPE);
×
72
                $envelope->setName($name);
×
73
                $envelope->setUuid($uuid);
×
74
                $envelope->setCreatedAt(new DateTime());
×
NEW
75
                $envelope->setStatusEnum(FileStatus::DRAFT);
×
76

77

78
                $envelope->setMetadata(['filesCount' => $filesCount]);
×
79

80
                if ($userId) {
×
81
                        $envelope->setUserId($userId);
×
82
                }
83

84
                return $this->fileMapper->insert($envelope);
×
85
        }
86

87
        public function addFileToEnvelope(int $envelopeId, FileEntity $file): FileEntity {
88
                $envelope = $this->fileMapper->getById($envelopeId);
×
89

90
                if (!$envelope->isEnvelope()) {
×
91
                        throw new LibresignException($this->l10n->t('The specified ID is not an envelope'));
×
92
                }
93

NEW
94
                if ($envelope->getStatus() > FileStatus::DRAFT->value) {
×
95
                        throw new LibresignException($this->l10n->t('Cannot add files to an envelope that is already in signing process'));
×
96
                }
97

98
                $maxFiles = $this->getMaxFilesPerEnvelope();
×
99
                $currentCount = $this->fileMapper->countChildrenFiles($envelopeId);
×
100
                if ($currentCount >= $maxFiles) {
×
101
                        throw new LibresignException(
×
102
                                $this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles])
×
103
                        );
×
104
                }
105

106
                $file->setParentFileId($envelopeId);
×
107
                $file->setNodeTypeEnum(NodeType::FILE);
×
108

109
                return $this->fileMapper->update($file);
×
110
        }
111

112
        public function getEnvelopeByFileId(int $fileId): ?FileEntity {
113
                try {
114
                        return $this->fileMapper->getParentEnvelope($fileId);
×
115
                } catch (DoesNotExistException) {
×
116
                        return null;
×
117
                }
118
        }
119

120
        public function getEnvelopeFolder(FileEntity $envelope): \OCP\Files\Folder {
121
                $userId = $envelope->getUserId();
×
122
                if (!$userId) {
×
123
                        throw new LibresignException('Envelope does not have a user');
×
124
                }
125

126
                $this->folderService->setUserId($userId);
×
127
                $userRootFolder = $this->folderService->getUserRootFolder();
×
128

129
                $envelopeFolderNode = $userRootFolder->getFirstNodeById($envelope->getNodeId());
×
130
                if (!$envelopeFolderNode instanceof \OCP\Files\Folder) {
×
131
                        throw new LibresignException('Envelope folder not found');
×
132
                }
133

134
                return $envelopeFolderNode;
×
135
        }
136

137
        private function getMaxFilesPerEnvelope(): int {
138
                return $this->appConfig->getValueInt(Application::APP_ID, 'envelope_max_files', 50);
×
139
        }
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc