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

LibreSign / libresign / 21500367177

30 Jan 2026 12:54AM UTC coverage: 46.602%. First build
21500367177

Pull #6641

github

web-flow
Merge cb4c40590 into f39fc2360
Pull Request #6641: refactor: centralize file status management

75 of 104 new or added lines in 13 files covered. (72.12%)

7906 of 16965 relevant lines covered (46.6%)

5.11 hits per line

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

89.83
/lib/Service/SignRequest/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\SignRequest;
10

11
use OCA\Libresign\Db\SignRequest as SignRequestEntity;
12
use OCA\Libresign\Db\SignRequestMapper;
13
use OCA\Libresign\Enum\SignRequestStatus;
14
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
15
use OCA\Libresign\Service\IdentifyMethodService;
16
use OCP\AppFramework\Db\DoesNotExistException;
17
use OCP\IL10N;
18
use OCP\IUserManager;
19
use Sabre\DAV\UUIDUtil;
20

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

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

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

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

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

70
                if ($isNewSignRequest
13✔
NEW
71
                        || $currentStatus === SignRequestStatus::DRAFT
×
72
                        || $currentStatus === SignRequestStatus::ABLE_TO_SIGN
13✔
73
                ) {
74
                        $desiredStatus = $this->signRequestStatusService->determineInitialStatus(
13✔
75
                                $signingOrder,
13✔
76
                                $fileId,
13✔
77
                                $fileStatus,
13✔
78
                                $signerStatus,
13✔
79
                                $currentStatus
13✔
80
                        );
13✔
81
                        $this->signRequestStatusService->updateStatusIfAllowed($signRequest, $currentStatus, $desiredStatus, $isNewSignRequest);
13✔
82
                }
83

84
                $this->insertOrUpdateSignRequest($signRequest);
13✔
85

86
                $shouldNotify = $notify && $this->signRequestStatusService->shouldNotifySignRequest(
13✔
87
                        $signRequest->getStatusEnum(),
13✔
88
                        $fileStatus
13✔
89
                );
13✔
90

91
                foreach ($identifyMethodsInstances as $identifyMethod) {
13✔
92
                        $identifyMethod->getEntity()->setSignRequestId($signRequest->getId());
13✔
93
                        $identifyMethod->willNotifyUser($shouldNotify);
13✔
94
                        $identifyMethod->save();
13✔
95
                }
96

97
                return $signRequest;
13✔
98
        }
99

100
        public function getSignRequestByIdentifyMethod(IIdentifyMethod $identifyMethod, int $fileId): SignRequestEntity {
101
                try {
102
                        $signRequest = $this->signRequestMapper->getByIdentifyMethodAndFileId($identifyMethod, $fileId);
13✔
103
                } catch (DoesNotExistException) {
13✔
104
                        $signRequest = new SignRequestEntity();
13✔
105
                }
106
                return $signRequest;
13✔
107
        }
108

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

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

154
        public function insertOrUpdateSignRequest(SignRequestEntity $signRequest): void {
155
                if ($signRequest->getId()) {
13✔
156
                        $this->signRequestMapper->update($signRequest);
×
157
                } else {
158
                        $this->signRequestMapper->insert($signRequest);
13✔
159
                }
160
        }
161
}
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