• 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

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\NodeType;
16
use OCA\Libresign\Exception\LibresignException;
17
use OCP\AppFramework\Db\DoesNotExistException;
18
use OCP\IAppConfig;
19
use OCP\IL10N;
20
use Sabre\DAV\UUIDUtil;
21

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

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

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

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

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

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

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

76
                $envelope->setMetadata(['filesCount' => $filesCount]);
×
77

78
                if ($userId) {
×
79
                        $envelope->setUserId($userId);
×
80
                }
81

82
                return $this->fileMapper->insert($envelope);
×
83
        }
84

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

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

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

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

104
                $file->setParentFileId($envelopeId);
×
105
                $file->setNodeTypeEnum(NodeType::FILE);
×
106

107
                return $this->fileMapper->update($file);
×
108
        }
109

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

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

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

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

132
                return $envelopeFolderNode;
×
133
        }
134

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