Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileDataAssembler
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 assemble
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
306
1<?php
2
3declare(strict_types=1);
4
5/**
6 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
7 * SPDX-License-Identifier: AGPL-3.0-or-later
8 */
9
10namespace OCA\Libresign\Service\File;
11
12use DateTimeInterface;
13use OCA\Libresign\Db\File;
14use OCA\Libresign\Db\FileMapper;
15use OCA\Libresign\Db\SignRequestMapper;
16use OCA\Libresign\Enum\FileStatus;
17use OCA\Libresign\Enum\SignatureFlow;
18use OCA\Libresign\Service\FileElementService;
19use OCA\Libresign\Service\FileService;
20use OCP\IURLGenerator;
21use OCP\IUserManager;
22
23class FileDataAssembler {
24    public function __construct(
25        private FileMapper $fileMapper,
26        private SignRequestMapper $signRequestMapper,
27        private FileElementService $fileElementService,
28        private EnvelopeAssembler $envelopeAssembler,
29        private IURLGenerator $urlGenerator,
30        private IUserManager $userManager,
31        private FileService $fileService,
32    ) {
33    }
34
35    public function assemble(File $file, \stdClass $fileData, FileResponseOptions $options): void {
36        $fileData->id = $file->getId();
37        $fileData->uuid = $file->getUuid();
38        $fileData->name = $file->getName();
39        $fileData->status = $file->getStatus();
40        $fileData->created_at = $file->getCreatedAt()->format(DateTimeInterface::ATOM);
41        $fileData->statusText = $this->fileMapper->getTextOfStatus($file->getStatus());
42        $fileData->nodeId = $file->getNodeId();
43        $fileData->signatureFlow = SignatureFlow::fromNumeric($file->getSignatureFlow())->value;
44        $fileData->docmdpLevel = $file->getDocmdpLevel();
45        $fileData->nodeType = $file->getNodeType();
46
47        if ($fileData->nodeType !== 'envelope' && !$file->getParentFileId()) {
48            $fileId = $file->getId();
49            $childrenFiles = $this->fileMapper->getChildrenFiles($fileId);
50            if (!empty($childrenFiles)) {
51                $file->setNodeType('envelope');
52                $this->fileService->update($file);
53
54                $fileData->nodeType = 'envelope';
55                $fileData->filesCount = count($childrenFiles);
56                $fileData->files = [];
57            }
58        }
59
60        if ($fileData->nodeType === 'envelope') {
61            $metadata = $file->getMetadata();
62            $fileData->filesCount = $metadata['filesCount'] ?? 0;
63            $fileData->files = [];
64            $childrenFiles = $this->fileMapper->getChildrenFiles($file->getId());
65            foreach ($childrenFiles as $childFile) {
66                $fileData->files[] = $this->envelopeAssembler->buildEnvelopeChildData($childFile, $options);
67            }
68            if ($file->getStatus() === FileStatus::SIGNED->value) {
69                $latestSignedDate = null;
70                foreach ($childrenFiles as $childFile) {
71                    $signRequests = $this->signRequestMapper->getByFileId($childFile->getId());
72                    foreach ($signRequests as $signRequest) {
73                        $signed = $signRequest->getSigned();
74                        if ($signed && (!$latestSignedDate || $signed > $latestSignedDate)) {
75                            $latestSignedDate = $signed;
76                        }
77                    }
78                }
79                if ($latestSignedDate) {
80                    $fileData->signedDate = $latestSignedDate->format(DateTimeInterface::ATOM);
81                }
82            }
83        }
84
85        $fileData->requested_by = [
86            'userId' => $file->getUserId(),
87            'displayName' => $this->userManager->get($file->getUserId())->getDisplayName(),
88        ];
89        $fileData->file = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $file->getUuid()]);
90
91        if ($options->isShowVisibleElements()) {
92            // For envelopes, the visibleElements are in the child files (buildEnvelopeChildData)
93            if ($fileData->nodeType === 'envelope') {
94                // The visibleElements of each child file are already loaded in the EnvelopeAssembler
95                // No need to duplicate the logic here
96                return;
97            }
98
99            // For individual files, fetch their visibleElements
100            $signers = $this->signRequestMapper->getByFileId($file->getId());
101            $fileData->visibleElements = [];
102            foreach ($this->signRequestMapper->getVisibleElementsFromSigners($signers) as $row) {
103                if (empty($row)) {
104                    continue;
105                }
106                $fileMetadata = $file->getMetadata();
107                $fileData->visibleElements = array_merge(
108                    $this->fileElementService->formatVisibleElements($row, $fileMetadata),
109                    $fileData->visibleElements
110                );
111            }
112        }
113    }
114}