Skip to content

Commit 108f5cd

Browse files
committed
fix: respect openapi contract
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 7e15058 commit 108f5cd

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

lib/Controller/FileController.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@
4949

5050
/**
5151
* @psalm-import-type LibresignFile from ResponseDefinitions
52-
* @psalm-import-type LibresignNewFile from ResponseDefinitions
52+
* @psalm-import-type LibresignFileDetail from ResponseDefinitions
5353
* @psalm-import-type LibresignFolderSettings from ResponseDefinitions
54+
* @psalm-import-type LibresignNewFile from ResponseDefinitions
55+
* @psalm-import-type LibresignNextcloudFile from ResponseDefinitions
5456
* @psalm-import-type LibresignNextcloudFile from ResponseDefinitions
5557
* @psalm-import-type LibresignPagination from ResponseDefinitions
5658
* @psalm-import-type LibresignSettings from ResponseDefinitions
57-
* @psalm-import-type LibresignFileDetail from ResponseDefinitions
59+
* @psalm-import-type LibresignSigner from ResponseDefinitions
5860
* @psalm-import-type LibresignSigner from ResponseDefinitions
5961
* @psalm-import-type LibresignValidateFile from ResponseDefinitions
62+
* @psalm-import-type LibresignValidateMetadata from ResponseDefinitions
63+
* @psalm-import-type LibresignValidateMetadata from ResponseDefinitions
64+
* @psalm-import-type LibresignVisibleElement from ResponseDefinitions
65+
* @psalm-import-type LibresignVisibleElement from ResponseDefinitions
6066
*/
6167
class FileController extends AEnvironmentAwareController {
6268
public function __construct(
@@ -651,6 +657,23 @@ private function saveFiles(array $files, string $name, array $settings): DataRes
651657
* @return DataResponse<Http::STATUS_OK, LibresignNextcloudFile, array{}>
652658
*/
653659
private function formatFileResponse(FileEntity $mainEntity, array $childFiles): DataResponse {
660+
$rawMetadata = $mainEntity->getMetadata() ?? [];
661+
$rawMetadata['extension'] = (string)($rawMetadata['extension'] ?? pathinfo($mainEntity->getName(), PATHINFO_EXTENSION));
662+
$rawMetadata['p'] = isset($rawMetadata['p']) ? (int)$rawMetadata['p'] : 0;
663+
/** @psalm-var LibresignValidateMetadata $metadata */
664+
$metadata = $rawMetadata;
665+
666+
/** @psalm-var list<LibresignVisibleElement> $visibleElements */
667+
$visibleElements = [];
668+
/** @psalm-var list<LibresignSigner> $signers */
669+
$signers = [];
670+
671+
$rawFilesCount = $rawMetadata['filesCount'] ?? null;
672+
$filesCount = is_numeric($rawFilesCount) ? (int)$rawFilesCount : count($childFiles);
673+
$filesCount = max(0, $filesCount);
674+
/** @var int<0, max> $filesCount */
675+
676+
/** @psalm-var LibresignNextcloudFile $response */
654677
$response = [
655678
'message' => $this->l10n->t('Success'),
656679
'id' => $mainEntity->getId(),
@@ -662,19 +685,18 @@ private function formatFileResponse(FileEntity $mainEntity, array $childFiles):
662685
'nodeType' => $mainEntity->getNodeType(),
663686
'created_at' => $mainEntity->getCreatedAt()->format(\DateTimeInterface::ATOM),
664687
'file' => $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $mainEntity->getUuid()]),
665-
'metadata' => $mainEntity->getMetadata() ?? [],
688+
'metadata' => $metadata,
666689
'signatureFlow' => SignatureFlow::fromNumeric($mainEntity->getSignatureFlow())->value,
667-
'visibleElements' => [],
668-
'signers' => [],
690+
'visibleElements' => $visibleElements,
691+
'signers' => $signers,
669692
'requested_by' => [
670693
'userId' => $mainEntity->getUserId(),
671694
'displayName' => $this->userSession->getUser()?->getDisplayName() ?? $mainEntity->getUserId(),
672695
],
673696
];
674697

675698
if ($mainEntity->getNodeType() === 'envelope') {
676-
$metadata = $mainEntity->getMetadata();
677-
$response['filesCount'] = $metadata['filesCount'] ?? count($childFiles);
699+
$response['filesCount'] = $filesCount;
678700
$response['files'] = !empty($childFiles) ? $this->formatFilesResponse($childFiles) : [];
679701
} else {
680702
$response['filesCount'] = 1;

lib/ResponseDefinitions.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,6 @@
4343
* name?: string,
4444
* type?: string,
4545
* }
46-
* @psalm-type LibresignNextcloudFile = array{
47-
* message: string,
48-
* name: non-falsy-string,
49-
* id: int,
50-
* nodeId: int,
51-
* uuid: string,
52-
* status: int,
53-
* statusText: string,
54-
* nodeType: 'file'|'envelope',
55-
* created_at: string,
56-
* file: string,
57-
* metadata: LibresignValidateMetadata,
58-
* signatureFlow: 'none'|'parallel'|'ordered_numeric',
59-
* visibleElements: LibresignVisibleElement[],
60-
* signers: LibresignSigner[],
61-
* requested_by: array{
62-
* userId: string,
63-
* displayName: string,
64-
* },
65-
* filesCount: int<0, max>,
66-
* files: list<array{nodeId: int, uuid: string, name: string, status: int, statusText: string}>,
67-
* }
6846
* @psalm-type LibresignIdentifyAccount = array{
6947
* id: non-negative-int,
7048
* isNoUser: boolean,
@@ -260,6 +238,28 @@
260238
* }[],
261239
* visibleElements?: LibresignVisibleElement[],
262240
* }
241+
* @psalm-type LibresignNextcloudFile = array{
242+
* message: string,
243+
* name: non-falsy-string,
244+
* id: int,
245+
* nodeId: int,
246+
* uuid: string,
247+
* status: int,
248+
* statusText: string,
249+
* nodeType: 'file'|'envelope',
250+
* created_at: string,
251+
* file: string,
252+
* metadata: LibresignValidateMetadata,
253+
* signatureFlow: 'none'|'parallel'|'ordered_numeric',
254+
* visibleElements: LibresignVisibleElement[],
255+
* signers: LibresignSigner[],
256+
* requested_by: array{
257+
* userId: string,
258+
* displayName: string,
259+
* },
260+
* filesCount: int<0, max>,
261+
* files: list<array{nodeId: int, uuid: string, name: string, status: int, statusText: string}>,
262+
* }
263263
* @psalm-type LibresignFileDetail = array{
264264
* created_at: string,
265265
* file: string,

0 commit comments

Comments
 (0)