Skip to content

Commit 9e8f6d4

Browse files
committed
refactor: replace generic file resolvers
Signed-off-by: Vitor Mattos <[email protected]>
1 parent c700165 commit 9e8f6d4

5 files changed

Lines changed: 45 additions & 33 deletions

File tree

lib/Controller/FileController.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,22 @@ private function validate(?string $type = null, $identifier = null): DataRespons
188188
try {
189189
if ($type === 'Uuid' && !empty($identifier)) {
190190
try {
191-
$this->fileService
192-
->setFileByType('Uuid', $identifier);
191+
$this->fileService->setFileByUuid((string)$identifier);
193192
} catch (LibresignException) {
194-
$this->fileService
195-
->setFileByType('SignerUuid', $identifier);
193+
$this->fileService->setFileBySignerUuid((string)$identifier);
196194
}
197-
} elseif (!empty($type) && !empty($identifier)) {
198-
$this->fileService
199-
->setFileByType($type, $identifier);
200-
195+
} elseif ($type === 'SignerUuid' && !empty($identifier)) {
196+
$this->fileService->setFileBySignerUuid((string)$identifier);
197+
} elseif ($type === 'FileId' && !empty($identifier)) {
198+
$this->fileService->setFileById((int)$identifier);
201199
} elseif ($this->request->getParam('fileId')) {
202-
$this->fileService->setFileByType(
203-
'FileId',
204-
$this->request->getParam('fileId')
205-
);
200+
$this->fileService->setFileById((int)$this->request->getParam('fileId'));
206201
} elseif ($this->request->getParam('uuid')) {
207-
$this->fileService->setFileByType(
208-
'Uuid',
209-
$this->request->getParam('uuid')
210-
);
202+
try {
203+
$this->fileService->setFileByUuid((string)$this->request->getParam('uuid'));
204+
} catch (LibresignException) {
205+
$this->fileService->setFileBySignerUuid((string)$this->request->getParam('uuid'));
206+
}
211207
}
212208

213209
$return = $this->fileService

lib/Controller/PageController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function indexFPath(string $path): TemplateResponse {
190190
try {
191191
$this->initialState->provideInitialState('file_info',
192192
$this->fileService
193-
->setFileByType('uuid', $matches['uuid'])
193+
->setFileByUuid($matches['uuid'])
194194
->setIdentifyMethodId($this->sessionService->getIdentifyMethodId())
195195
->setHost($this->request->getServerHost())
196196
->setMe($this->userSession->getUser())
@@ -635,7 +635,7 @@ public function resetPassword(): TemplateResponse {
635635
public function validationFilePublic(string $uuid): TemplateResponse {
636636
try {
637637
$this->signFileService->getFileByUuid($uuid);
638-
$this->fileService->setFileByType('uuid', $uuid);
638+
$this->fileService->setFileByUuid($uuid);
639639
} catch (DoesNotExistException) {
640640
try {
641641
$signRequest = $this->signFileService->getSignRequestByUuid($uuid);

lib/Dav/SignatureStatusPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function propFind(PropFind $propFind, INode $node): void {
3232
return;
3333
}
3434

35-
$fileService->setFileByType('FileId', $nodeId);
35+
$fileService->setFileByNodeId($nodeId);
3636

3737
$propFind->handle('{http://nextcloud.org/ns}libresign-signature-status', $fileService->getStatus());
3838
$propFind->handle('{http://nextcloud.org/ns}libresign-signed-node-id', $fileService->getSignedNodeId());

lib/Service/FileService.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,34 @@ public function showValidateFile(bool $validateFile = true): self {
228228
return $this;
229229
}
230230

231-
/**
232-
* @return static
233-
*/
234-
public function setFileByType(string $type, $identifier): self {
231+
private function setFileOrFail(callable $resolver): self {
235232
try {
236-
$method = $type === 'FileId' ? 'getById' : 'getBy' . $type;
237-
/** @var File */
238-
$file = call_user_func([$this->fileMapper, $method], $identifier);
233+
$file = $resolver();
239234
} catch (\Throwable) {
240235
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404);
241236
}
242-
if (!$file) {
237+
238+
if (!$file instanceof File) {
243239
throw new LibresignException($this->l10n->t('Invalid file identifier'), 404);
244240
}
245-
$this->setFile($file);
246-
return $this;
241+
242+
return $this->setFile($file);
243+
}
244+
245+
public function setFileById(int $fileId): self {
246+
return $this->setFileOrFail(fn () => $this->fileMapper->getById($fileId));
247+
}
248+
249+
public function setFileByUuid(string $uuid): self {
250+
return $this->setFileOrFail(fn () => $this->fileMapper->getByUuid($uuid));
251+
}
252+
253+
public function setFileBySignerUuid(string $uuid): self {
254+
return $this->setFileOrFail(fn () => $this->fileMapper->getBySignerUuid($uuid));
255+
}
256+
257+
public function setFileByNodeId(int $nodeId): self {
258+
return $this->setFileOrFail(fn () => $this->fileMapper->getByNodeId($nodeId));
247259
}
248260

249261
public function validateUploadedFile(array $file): void {
@@ -302,6 +314,10 @@ public function getStatus(): int {
302314
return $this->file->getStatus();
303315
}
304316

317+
public function isLibresignFile(int $nodeId): bool {
318+
return $this->fileMapper->fileIdExists($nodeId);
319+
}
320+
305321
public function getSignedNodeId(): ?int {
306322
$status = $this->file->getStatus();
307323

tests/php/Unit/Service/FileServiceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testValidateFileContentSkipsNonPdfFiles(): void {
6161
$service->validateFileContent('{"json": true}', 'json');
6262
}
6363

64-
public function testSetFileByTypeThrowsOnInvalid(): void {
64+
public function testSetFileByIdThrowsOnInvalid(): void {
6565
$fileMapper = $this->createMock(\OCA\Libresign\Db\FileMapper::class);
6666
$fileMapper->method('getById')->willThrowException(new \Exception('not found'));
6767

@@ -70,10 +70,10 @@ public function testSetFileByTypeThrowsOnInvalid(): void {
7070
]);
7171

7272
$this->expectException(LibresignException::class);
73-
$service->setFileByType('FileId', 123);
73+
$service->setFileById(123);
7474
}
7575

76-
public function testSetFileByTypeSetsFile(): void {
76+
public function testSetFileByIdSetsFile(): void {
7777
$file = new \OCA\Libresign\Db\File();
7878
$file->setStatus(1);
7979

@@ -84,7 +84,7 @@ public function testSetFileByTypeSetsFile(): void {
8484
\OCA\Libresign\Db\FileMapper::class => $fileMapper,
8585
]);
8686

87-
$returned = $service->setFileByType('FileId', 123);
87+
$returned = $service->setFileById(123);
8888
$this->assertInstanceOf(FileService::class, $returned);
8989
$this->assertSame(1, $service->getStatus());
9090
}

0 commit comments

Comments
 (0)