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

LibreSign / libresign / 20488765679

24 Dec 2025 03:03PM UTC coverage: 42.73%. First build
20488765679

Pull #6242

github

web-flow
Merge ebcd2aa70 into c1c3239d5
Pull Request #6242: feat: envelope support

367 of 1066 new or added lines in 23 files covered. (34.43%)

6171 of 14442 relevant lines covered (42.73%)

4.98 hits per line

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

91.23
/lib/Service/SignRequestService.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 OCA\Libresign\Db\SignRequest as SignRequestEntity;
12
use OCA\Libresign\Db\SignRequestMapper;
13
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
14
use OCP\AppFramework\Db\DoesNotExistException;
15
use OCP\IL10N;
16
use OCP\IUserManager;
17
use Sabre\DAV\UUIDUtil;
18

19
class SignRequestService {
20
        public function __construct(
21
                protected IL10N $l10n,
22
                protected SignRequestMapper $signRequestMapper,
23
                protected IUserManager $userManager,
24
                protected IdentifyMethodService $identifyMethodService,
25
                protected SignRequestStatusService $signRequestStatusService,
26
        ) {
27
        }
40✔
28

29
        /**
30
         * Create or update a sign request for a signer
31
         *
32
         * @param array $identifyMethods Identify methods array
33
         * @param string $displayName Signer display name
34
         * @param string $description Signer description
35
         * @param bool $notify Whether to notify the signer
36
         * @param int $fileId File ID
37
         * @param int $signingOrder Signing order
38
         * @param int|null $fileStatus File status
39
         * @param int|null $signerStatus Signer status
40
         * @return SignRequestEntity
41
         */
42
        public function createOrUpdateSignRequest(
43
                array $identifyMethods,
44
                string $displayName,
45
                string $description,
46
                bool $notify,
47
                int $fileId,
48
                int $signingOrder = 0,
49
                ?int $fileStatus = null,
50
                ?int $signerStatus = null,
51
        ): SignRequestEntity {
52
                $identifyMethodsInstances = $this->identifyMethodService->getByUserData($identifyMethods);
13✔
53
                if (empty($identifyMethodsInstances)) {
13✔
NEW
54
                        throw new \Exception($this->l10n->t('Invalid identification method'));
×
55
                }
56

57
                $signRequest = $this->getSignRequestByIdentifyMethod(
13✔
58
                        current($identifyMethodsInstances),
13✔
59
                        $fileId
13✔
60
                );
13✔
61

62
                $displayName = $this->getDisplayNameFromIdentifyMethodIfEmpty($identifyMethodsInstances, $displayName);
13✔
63
                $this->populateSignRequest($signRequest, $displayName, $signingOrder, $description, $fileId);
13✔
64

65
                $isNewSignRequest = !$signRequest->getId();
13✔
66
                $currentStatus = $signRequest->getStatusEnum();
13✔
67

68
                if ($isNewSignRequest || $currentStatus === \OCA\Libresign\Enum\SignRequestStatus::DRAFT) {
13✔
69
                        $desiredStatus = $this->signRequestStatusService->determineInitialStatus(
13✔
70
                                $signingOrder,
13✔
71
                                $fileId,
13✔
72
                                $fileStatus,
13✔
73
                                $signerStatus,
13✔
74
                                $currentStatus
13✔
75
                        );
13✔
76
                        $this->signRequestStatusService->updateStatusIfAllowed($signRequest, $currentStatus, $desiredStatus, $isNewSignRequest);
13✔
77
                }
78

79
                $this->insertOrUpdateSignRequest($signRequest);
13✔
80

81
                $shouldNotify = $notify && $this->signRequestStatusService->shouldNotifySignRequest(
13✔
82
                        $signRequest->getStatusEnum(),
13✔
83
                        $fileStatus
13✔
84
                );
13✔
85

86
                foreach ($identifyMethodsInstances as $identifyMethod) {
13✔
87
                        $identifyMethod->getEntity()->setSignRequestId($signRequest->getId());
13✔
88
                        $identifyMethod->willNotifyUser($shouldNotify);
13✔
89
                        $identifyMethod->save();
13✔
90
                }
91

92
                return $signRequest;
13✔
93
        }
94

95
        public function getSignRequestByIdentifyMethod(IIdentifyMethod $identifyMethod, int $fileId): SignRequestEntity {
96
                try {
97
                        $signRequest = $this->signRequestMapper->getByIdentifyMethodAndFileId($identifyMethod, $fileId);
13✔
98
                } catch (DoesNotExistException) {
13✔
99
                        $signRequest = new SignRequestEntity();
13✔
100
                }
101
                return $signRequest;
13✔
102
        }
103

104
        private function populateSignRequest(
105
                SignRequestEntity $signRequest,
106
                string $displayName,
107
                int $signingOrder,
108
                string $description,
109
                int $fileId,
110
        ): void {
111
                $signRequest->setFileId($fileId);
13✔
112
                $signRequest->setSigningOrder($signingOrder);
13✔
113
                if (!$signRequest->getUuid()) {
13✔
114
                        $signRequest->setUuid(UUIDUtil::getUUID());
13✔
115
                }
116
                if (!empty($displayName)) {
13✔
117
                        $signRequest->setDisplayName($displayName);
13✔
118
                }
119
                if (!empty($description)) {
13✔
NEW
120
                        $signRequest->setDescription($description);
×
121
                }
122
                if (!$signRequest->getId()) {
13✔
123
                        $signRequest->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
13✔
124
                }
125
        }
126

127
        /**
128
         * @param IIdentifyMethod[] $identifyMethodsInstances
129
         * @param string $displayName
130
         * @return string
131
         */
132
        private function getDisplayNameFromIdentifyMethodIfEmpty(array $identifyMethodsInstances, string $displayName): string {
133
                if (!empty($displayName)) {
13✔
NEW
134
                        return $displayName;
×
135
                }
136
                foreach ($identifyMethodsInstances as $identifyMethod) {
13✔
137
                        if ($identifyMethod->getName() === 'account') {
13✔
138
                                return $this->userManager->get($identifyMethod->getEntity()->getIdentifierValue())->getDisplayName();
2✔
139
                        }
140
                }
141
                foreach ($identifyMethodsInstances as $identifyMethod) {
11✔
142
                        if ($identifyMethod->getName() !== 'account') {
11✔
143
                                return $identifyMethod->getEntity()->getIdentifierValue();
11✔
144
                        }
145
                }
NEW
146
                return '';
×
147
        }
148

149
        public function insertOrUpdateSignRequest(SignRequestEntity $signRequest): void {
150
                if ($signRequest->getId()) {
13✔
NEW
151
                        $this->signRequestMapper->update($signRequest);
×
152
                } else {
153
                        $this->signRequestMapper->insert($signRequest);
13✔
154
                }
155
        }
156
}
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