Skip to content

Commit

Permalink
#test-app-fixes improve documentation and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
steinkel committed Mar 28, 2024
1 parent 9a9e7c4 commit 7d04085
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config/uppy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
return [
'Uppy' => [
'Props' => [
// the Table Alias to identify the user who uploaded the file
'usersAliasModel' => 'Users',
// the Table className to identify the user who uploaded the file
'usersModel' => 'Users',
'deleteFileS3' => true,
'tableFiles' => 'uppy_files',
Expand Down
18 changes: 10 additions & 8 deletions src/Controller/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ public function view(string|int|null $id = null): ?Response
public function save(): void
{
$this->getRequest()->allowMethod('post');
$this->viewBuilder()->setClassName('Json');

$items = $this->getRequest()->getData('items');

$files = [];
$result = [];
foreach ($items as $item) {
if (!isset($item['model'])) {
$tableAlias = $item['model'] ?? null;
if (!$tableAlias) {
$result['error'] = true;
$result['message'] = __('model is required');
$this->set('result', $result);
Expand All @@ -95,16 +97,17 @@ public function save(): void
return;
}
try {
$relationTable = $this->fetchTable($item['model']);
$relationTable = $this->fetchTable($tableAlias);
} catch (MissingTableClassException | UnexpectedValueException) {

Check failure on line 101 in src/Controller/FilesController.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Caught class CakeDC\Uppy\Controller\UnexpectedValueException not found.

Check failure on line 101 in src/Controller/FilesController.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

UndefinedClass

src/Controller/FilesController.php:101:51: UndefinedClass: Class, interface or enum named CakeDC\Uppy\Controller\UnexpectedValueException does not exist (see https://psalm.dev/019)
$result['error'] = true;
$result['message'] = __('there is no table {0} to associate the file', $item['model']);
$result['message'] = __('there is no table {0} to associate the file', $tableAlias);
$this->set('result', $result);
$this->viewBuilder()->setOption('serialize', ['result']);

return;
}
if (!isset($item['foreign_key'])) {
$foreignKey = $item['foreign_key'] ?? null;
if (!$foreignKey) {
$result['error'] = true;
$result['message'] = __('foreign key is required');
$this->set('result', $result);
Expand All @@ -113,10 +116,10 @@ public function save(): void
return;
}
try {
$register = $relationTable->get($item['foreign_key']);
$register = $relationTable->get($foreignKey);
} catch (RecordNotFoundException) {
$result['error'] = true;
$result['message'] = __('there is no record {0} to associate the file', $item['foreign_key']);
$result['message'] = __('there is no record with id {0} to associate the file', $foreignKey);
$this->set('result', $result);
$this->viewBuilder()->setOption('serialize', ['result']);

Expand All @@ -129,7 +132,7 @@ public function save(): void
$model = Configure::readOrFail('Uppy.Props.usersModel');
$relation_key = Inflector::singularize(mb_strtolower($model)) . '_id';
$file->user_id = $register->{$relation_key};
$file->model = $item['model'];
$file->model = $tableAlias;
$files[] = $file;
}

Expand All @@ -141,7 +144,6 @@ public function save(): void
$result['message'] = __('The association to file could not be saved');
}

$this->viewBuilder()->setClassName('Json');
$this->set('result', $result);
$this->viewBuilder()->setOption('serialize', ['result']);
}
Expand Down

0 comments on commit 7d04085

Please sign in to comment.