Skip to content

Commit 7c2774c

Browse files
committed
chore: make compatible with sign multiple files
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent d6d09af commit 7c2774c

File tree

1 file changed

+96
-8
lines changed

1 file changed

+96
-8
lines changed

lib/Service/SignFileService.php

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,106 @@ public function getVisibleElements(): array {
320320
return $this->elements;
321321
}
322322

323-
public function sign(): File {
324-
$this->validateDocMdpAllowsSignatures();
325-
$signedFile = $this->getEngine()->sign();
323+
public function sign(): void {
324+
$originalLibreSignFile = $this->libreSignFile;
325+
$originalSignRequest = $this->signRequest;
326326

327-
$hash = $this->computeHash($signedFile);
327+
$signRequests = $this->getSignRequestsToSign();
328328

329-
$this->updateSignRequest($hash);
330-
$this->updateLibreSignFile($hash);
329+
if (empty($signRequests)) {
330+
throw new LibresignException('No sign requests found to process');
331+
}
332+
333+
foreach ($signRequests as $signRequestData) {
334+
$this->libreSignFile = $signRequestData['file'];
335+
$this->signRequest = $signRequestData['signRequest'];
336+
$this->engine = null;
337+
$this->elements = [];
338+
339+
$this->validateDocMdpAllowsSignatures();
340+
$signedFile = $this->getEngine()->sign();
341+
342+
$hash = $this->computeHash($signedFile);
343+
344+
$this->updateSignRequest($hash);
345+
$this->updateLibreSignFile($hash);
346+
347+
$this->dispatchSignedEvent();
348+
}
349+
350+
$this->libreSignFile = $originalLibreSignFile;
351+
$this->signRequest = $originalSignRequest;
352+
353+
if ($originalLibreSignFile->isEnvelope()) {
354+
$this->updateEnvelopeStatus();
355+
}
356+
}
357+
358+
/**
359+
* @return array Array of ['file' => FileEntity, 'signRequest' => SignRequestEntity]
360+
*/
361+
private function getSignRequestsToSign(): array {
362+
if (!$this->libreSignFile->isEnvelope()) {
363+
return [[
364+
'file' => $this->libreSignFile,
365+
'signRequest' => $this->signRequest,
366+
]];
367+
}
331368

332-
$this->dispatchSignedEvent();
369+
$childFiles = $this->fileMapper->getChildrenFiles($this->libreSignFile->getId());
370+
371+
if (empty($childFiles)) {
372+
throw new LibresignException('No files found in envelope');
373+
}
374+
375+
$childSignRequests = $this->signRequestMapper->getByEnvelopeChildrenAndIdentifyMethod(
376+
$this->libreSignFile->getId(),
377+
$this->signRequest->getId()
378+
);
333379

334-
return $signedFile;
380+
if (empty($childSignRequests)) {
381+
throw new LibresignException('No sign requests found for envelope files');
382+
}
383+
384+
$signRequestsData = [];
385+
foreach ($childSignRequests as $childSignRequest) {
386+
$childFile = $this->array_find(
387+
$childFiles,
388+
fn(FileEntity $file) => $file->getId() === $childSignRequest->getFileId()
389+
);
390+
391+
if ($childFile) {
392+
$signRequestsData[] = [
393+
'file' => $childFile,
394+
'signRequest' => $childSignRequest,
395+
];
396+
}
397+
}
398+
399+
return $signRequestsData;
400+
}
401+
402+
private function updateEnvelopeStatus(): void {
403+
$childFiles = $this->fileMapper->getChildrenFiles($this->libreSignFile->getId());
404+
405+
$allSigned = true;
406+
$anySigned = false;
407+
408+
foreach ($childFiles as $childFile) {
409+
if ($childFile->getStatus() === FileEntity::STATUS_SIGNED) {
410+
$anySigned = true;
411+
} else {
412+
$allSigned = false;
413+
}
414+
}
415+
416+
if ($allSigned) {
417+
$this->libreSignFile->setStatus(FileEntity::STATUS_SIGNED);
418+
} elseif ($anySigned) {
419+
$this->libreSignFile->setStatus(FileEntity::STATUS_PARTIAL_SIGNED);
420+
}
421+
422+
$this->fileMapper->update($this->libreSignFile);
335423
}
336424

337425
/**

0 commit comments

Comments
 (0)