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

LibreSign / libresign / 20210417471

14 Dec 2025 03:51PM UTC coverage: 44.081%. First build
20210417471

Pull #6181

github

web-flow
Merge 21aee754a into 95a26cb92
Pull Request #6181: feat: cancel signature request notification

114 of 235 new or added lines in 9 files covered. (48.51%)

5932 of 13457 relevant lines covered (44.08%)

5.06 hits per line

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

36.26
/lib/Service/MailService.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2020-2024 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\File;
12
use OCA\Libresign\Db\FileMapper;
13
use OCA\Libresign\Db\SignRequest;
14
use OCA\Libresign\Exception\LibresignException;
15
use OCP\IAppConfig;
16
use OCP\IL10N;
17
use OCP\IURLGenerator;
18
use OCP\Mail\IMailer;
19
use Psr\Log\LoggerInterface;
20

21
class MailService {
22
        /** @var array */
23
        private $files = [];
24

25
        public function __construct(
26
                private LoggerInterface $logger,
27
                private IMailer $mailer,
28
                private FileMapper $fileMapper,
29
                private IL10N $l10n,
30
                private IURLGenerator $urlGenerator,
31
                private IAppConfig $appConfig,
32
        ) {
33
        }
49✔
34

35
        /**
36
         * @psalm-suppress MixedReturnStatement
37
         */
38
        private function getFileById(int $fileId): File {
39
                if (!isset($this->files[$fileId])) {
15✔
40
                        $this->files[$fileId] = $this->fileMapper->getById($fileId);
15✔
41
                }
42
                return $this->files[$fileId];
15✔
43
        }
44

45
        /**
46
         * @psalm-suppress MixedMethodCall
47
         */
48
        public function notifySignDataUpdated(SignRequest $data, string $email): void {
49
                $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
×
50
                // TRANSLATORS The subject of the email that is sent after changes are made to the signature request that may affect something for the signer who will sign the document. Some possible reasons: URL for signature changed (when the URL expires), the person who requested the signature sent a notification
51
                $emailTemplate->setSubject($this->l10n->t('LibreSign: Changes into a file for you to sign'));
×
52
                $emailTemplate->addHeader();
×
53
                $emailTemplate->addHeading($this->l10n->t('File to sign'), false);
×
54
                $emailTemplate->addBodyText($this->l10n->t('Changes have been made in a file that you have to sign. Access the link below:'));
×
55
                $link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
×
56
                $file = $this->getFileById($data->getFileId());
×
57
                $emailTemplate->addBodyButton(
×
58
                        $this->l10n->t('Sign »%s«', [$file->getName()]),
×
59
                        $link
×
60
                );
×
61
                $message = $this->mailer->createMessage();
×
62
                if ($data->getDisplayName()) {
×
63
                        $message->setTo([$email => $data->getDisplayName()]);
×
64
                } else {
65
                        $message->setTo([$email]);
×
66
                }
67
                $message->useTemplate($emailTemplate);
×
68
                try {
69
                        $this->mailer->send($message);
×
70
                } catch (\Exception $e) {
×
71
                        $this->logger->error('Notify changes in unsigned notification mail could not be sent: ' . $e->getMessage());
×
72
                        throw new LibresignException('Notify unsigned notification mail could not be sent', 1);
×
73
                }
74
        }
75

76
        /**
77
         * @psalm-suppress MixedMethodCall
78
         */
79
        public function notifyUnsignedUser(SignRequest $data, string $email): void {
80
                $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
15✔
81
                $emailTemplate->setSubject($this->l10n->t('LibreSign: There is a file for you to sign'));
15✔
82
                $emailTemplate->addHeader();
15✔
83
                $emailTemplate->addHeading($this->l10n->t('File to sign'), false);
15✔
84
                $emailTemplate->addBodyText($this->l10n->t('There is a document for you to sign. Access the link below:'));
15✔
85
                $link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.sign', ['uuid' => $data->getUuid()]);
15✔
86
                $file = $this->getFileById($data->getFileId());
15✔
87
                $emailTemplate->addBodyButton(
15✔
88
                        $this->l10n->t('Sign »%s«', [$file->getName()]),
15✔
89
                        $link
15✔
90
                );
15✔
91
                $message = $this->mailer->createMessage();
15✔
92
                if ($data->getDisplayName()) {
15✔
93
                        $message->setTo([$email => $data->getDisplayName()]);
15✔
94
                } else {
95
                        $message->setTo([$email]);
×
96
                }
97
                $message->useTemplate($emailTemplate);
15✔
98
                try {
99
                        $this->mailer->send($message);
15✔
100
                } catch (\Exception $e) {
1✔
101
                        $this->logger->error('Notify unsigned notification mail could not be sent: ' . $e->getMessage());
1✔
102
                        throw new LibresignException('Notify unsigned notification mail could not be sent', 1);
1✔
103
                }
104
        }
105

106
        public function notifySignedUser(SignRequest $signRequest, string $email, File $libreSignFile, string $displayName): void {
107
                $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
×
108
                // TRANSLATORS The subject of the email that is sent after a document has been signed by a user. This email is sent to the person who requested the signature.
109
                $emailTemplate->setSubject($this->l10n->t('LibreSign: A file has been signed'));
×
110
                $emailTemplate->addHeader();
×
111
                $emailTemplate->addHeading($this->l10n->t('File signed'), false);
×
112
                // TRANSLATORS The text in the email that is sent after a document has been signed by a user. %s will be replaced with the name of the user who signed the document.
113
                $emailTemplate->addBodyText($this->l10n->t('%s signed the document. You can access it using the link below:', [$signRequest->getDisplayName()]));
×
114
                $link = $this->urlGenerator->linkToRouteAbsolute('libresign.page.indexFPath', [
×
115
                        'path' => 'validation/' . $libreSignFile->getUuid(),
×
116
                ]);
×
117
                $file = $this->getFileById($signRequest->getFileId());
×
118
                $emailTemplate->addBodyButton(
×
119
                        // TRANSLATORS The button text in the email that is sent after a document has been signed by a user. %s will be replaced with the name of the file that was signed.
120
                        $this->l10n->t('View signed file »%s«', [$file->getName()]),
×
121
                        $link
×
122
                );
×
123
                $message = $this->mailer->createMessage();
×
124
                $message->setTo([$email => $displayName]);
×
125

126
                $message->useTemplate($emailTemplate);
×
127
                try {
128
                        $this->mailer->send($message);
×
129
                } catch (\Exception $e) {
×
130
                        $this->logger->error('Notify signed notification mail could not be sent: ' . $e->getMessage());
×
131
                        throw new LibresignException('Notify signed notification mail could not be sent', 1);
×
132
                }
133
        }
134

135
        public function notifyCanceledRequest(SignRequest $signRequest, string $email, File $libreSignFile): void {
136
                $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
2✔
137
                // TRANSLATORS The subject of the email that is sent when a signature request has been canceled.
138
                $emailTemplate->setSubject($this->l10n->t('LibreSign: A signature request has been canceled'));
2✔
139
                $emailTemplate->addHeader();
2✔
140
                $emailTemplate->addHeading($this->l10n->t('Signature request canceled'), false);
2✔
141
                // TRANSLATORS The text in the email that is sent when a signature request has been canceled. %s will be replaced with the name of the file.
142
                $emailTemplate->addBodyText($this->l10n->t('The request for you to sign "%s" has been canceled.', [$libreSignFile->getName()]));
2✔
143
                $message = $this->mailer->createMessage();
2✔
144
                if ($signRequest->getDisplayName()) {
2✔
145
                        $message->setTo([$email => $signRequest->getDisplayName()]);
2✔
146
                } else {
NEW
147
                        $message->setTo([$email]);
×
148
                }
149
                $message->useTemplate($emailTemplate);
2✔
150
                try {
151
                        $this->mailer->send($message);
2✔
NEW
152
                } catch (\Exception $e) {
×
NEW
153
                        $this->logger->error('Notify canceled request mail could not be sent: ' . $e->getMessage());
×
154
                        // Don't throw exception to avoid breaking the flow when mail fails
155
                }
156
        }
157

158
        public function sendCodeToSign(string $email, string $name, string $code): void {
159
                $emailTemplate = $this->mailer->createEMailTemplate('settings.TestEmail');
×
160
                $emailTemplate->setSubject($this->l10n->t('LibreSign: Code to sign file'));
×
161
                $emailTemplate->addHeader();
×
162
                $emailTemplate->addBodyText($this->l10n->t('Use this code to sign the document:'));
×
163
                $emailTemplate->addBodyText($code);
×
164
                $message = $this->mailer->createMessage();
×
165
                if (!empty($name)) {
×
166
                        $message->setTo([$email => $name]);
×
167
                } else {
168
                        $message->setTo([$email]);
×
169
                }
170
                $message->useTemplate($emailTemplate);
×
171
                try {
172
                        $this->mailer->send($message);
×
173
                } catch (\Exception $e) {
×
174
                        $this->logger->error('Mail with code to sign document could not be sent: ' . $e->getMessage());
×
175
                        throw new LibresignException('Mail with code to sign document could not be sent', 1);
×
176
                }
177
        }
178
}
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