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

LibreSign / libresign / 20660742393

02 Jan 2026 03:15PM UTC coverage: 44.876%. First build
20660742393

Pull #6302

github

web-flow
Merge 928956222 into cfd974a7d
Pull Request #6302: feat: envelope custom path

77 of 126 new or added lines in 6 files covered. (61.11%)

6660 of 14841 relevant lines covered (44.88%)

5.07 hits per line

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

81.48
/lib/Service/Envelope/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\Envelope;
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\NodeType;
16
use OCA\Libresign\Exception\LibresignException;
17
use OCA\Libresign\Service\FolderService;
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
        }
61✔
31

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

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

44
                $maxFiles = $this->getMaxFilesPerEnvelope();
5✔
45
                if ($fileCount > $maxFiles) {
5✔
46
                        throw new LibresignException(
2✔
47
                                $this->l10n->t('Maximum number of files per envelope (%s) exceeded', [$maxFiles])
2✔
48
                        );
2✔
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);
9✔
59

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

69
                $envelope = new FileEntity();
8✔
70
                $envelope->setNodeId($envelopeFolder->getId());
8✔
71
                $envelope->setNodeTypeEnum(NodeType::ENVELOPE);
8✔
72
                $envelope->setName($name);
8✔
73
                $envelope->setUuid($uuid);
8✔
74
                $envelope->setCreatedAt(new DateTime());
8✔
75
                $envelope->setStatus(FileEntity::STATUS_DRAFT);
8✔
76

77
                $envelope->setMetadata(['filesCount' => $filesCount]);
8✔
78

79
                if ($userId) {
8✔
80
                        $envelope->setUserId($userId);
8✔
81
                }
82

83
                return $this->fileMapper->insert($envelope);
8✔
84
        }
85

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

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

93
                if ($envelope->getStatus() > FileEntity::STATUS_DRAFT) {
4✔
94
                        throw new LibresignException($this->l10n->t('Cannot add files to an envelope that is already in signing process'));
1✔
95
                }
96

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

105
                $file->setParentFileId($envelopeId);
2✔
106
                $file->setNodeTypeEnum(NodeType::FILE);
2✔
107

108
                return $this->fileMapper->update($file);
2✔
109
        }
110

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

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

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

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

NEW
133
                return $envelopeFolderNode;
×
134
        }
135

136
        private function getMaxFilesPerEnvelope(): int {
137
                return $this->appConfig->getValueInt(Application::APP_ID, 'envelope_max_files', 50);
8✔
138
        }
139
}
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