chore: reformat code.

This commit is contained in:
James Cole
2023-06-21 12:34:58 +02:00
parent 8d87abde64
commit 3dcb35710b
799 changed files with 23319 additions and 22173 deletions

View File

@@ -70,7 +70,7 @@ class AttachmentHelper implements AttachmentHelperInterface
* Returns the content of an attachment.
*
*
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string
*/
@@ -90,7 +90,7 @@ class AttachmentHelper implements AttachmentHelperInterface
/**
* Returns the file path relative to upload disk for an attachment,
*
* @param Attachment $attachment
* @param Attachment $attachment
*
* @return string
*/
@@ -132,8 +132,8 @@ class AttachmentHelper implements AttachmentHelperInterface
/**
* Uploads a file as a string.
*
* @param Attachment $attachment
* @param string $content
* @param Attachment $attachment
* @param string $content
*
* @return bool
*/
@@ -181,8 +181,8 @@ class AttachmentHelper implements AttachmentHelperInterface
/**
* Save attachments that get uploaded with models, through the app.
*
* @param object $model
* @param array|null $files
* @param object $model
* @param array|null $files
*
* @return bool
* @throws FireflyException
@@ -211,44 +211,11 @@ class AttachmentHelper implements AttachmentHelperInterface
return true;
}
/**
* Check if a model already has this file attached.
*
* @param UploadedFile $file
* @param Model $model
*
* @return bool
*/
protected function hasFile(UploadedFile $file, Model $model): bool
{
$md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName();
$class = get_class($model);
$count = 0;
// ignore lines about polymorphic calls.
if ($model instanceof PiggyBank) {
$count = $model->account->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
}
if (!($model instanceof PiggyBank)) {
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(
); // @phpstan-ignore-line
}
$result = false;
if ($count > 0) {
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = true;
}
return $result;
}
/**
* Process the upload of a file.
*
* @param UploadedFile $file
* @param Model $model
* @param UploadedFile $file
* @param Model $model
*
* @return Attachment|null
* @throws FireflyException
@@ -301,62 +268,11 @@ class AttachmentHelper implements AttachmentHelperInterface
return $attachment;
}
/**
* Verify if the mime of a file is valid.
*
* @param UploadedFile $file
*
* @return bool
*/
protected function validMime(UploadedFile $file): bool
{
Log::debug('Now in validMime()');
$mime = e($file->getMimeType());
$name = e($file->getClientOriginalName());
Log::debug(sprintf('Name is %s, and mime is %s', $name, $mime));
Log::debug('Valid mimes are', $this->allowedMimes);
$result = true;
if (!in_array($mime, $this->allowedMimes, true)) {
$msg = (string)trans('validation.file_invalid_mime', ['name' => $name, 'mime' => $mime]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = false;
}
return $result;
}
/**
* Verify if the size of a file is valid.
*
*
* @param UploadedFile $file
*
* @return bool
*/
protected function validSize(UploadedFile $file): bool
{
$size = $file->getSize();
$name = e($file->getClientOriginalName());
$result = true;
if ($size > $this->maxUploadSize) {
$msg = (string)trans('validation.file_too_large', ['name' => $name]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = false;
}
return $result;
}
/**
* Verify if the file was uploaded correctly.
*
* @param UploadedFile $file
* @param Model $model
* @param UploadedFile $file
* @param Model $model
*
* @return bool
*/
@@ -384,4 +300,87 @@ class AttachmentHelper implements AttachmentHelperInterface
return $result;
}
/**
* Verify if the mime of a file is valid.
*
* @param UploadedFile $file
*
* @return bool
*/
protected function validMime(UploadedFile $file): bool
{
Log::debug('Now in validMime()');
$mime = e($file->getMimeType());
$name = e($file->getClientOriginalName());
Log::debug(sprintf('Name is %s, and mime is %s', $name, $mime));
Log::debug('Valid mimes are', $this->allowedMimes);
$result = true;
if (!in_array($mime, $this->allowedMimes, true)) {
$msg = (string)trans('validation.file_invalid_mime', ['name' => $name, 'mime' => $mime]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = false;
}
return $result;
}
/**
* Verify if the size of a file is valid.
*
*
* @param UploadedFile $file
*
* @return bool
*/
protected function validSize(UploadedFile $file): bool
{
$size = $file->getSize();
$name = e($file->getClientOriginalName());
$result = true;
if ($size > $this->maxUploadSize) {
$msg = (string)trans('validation.file_too_large', ['name' => $name]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = false;
}
return $result;
}
/**
* Check if a model already has this file attached.
*
* @param UploadedFile $file
* @param Model $model
*
* @return bool
*/
protected function hasFile(UploadedFile $file, Model $model): bool
{
$md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName();
$class = get_class($model);
$count = 0;
// ignore lines about polymorphic calls.
if ($model instanceof PiggyBank) {
$count = $model->account->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
}
if (!($model instanceof PiggyBank)) {
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(); // @phpstan-ignore-line
}
$result = false;
if ($count > 0) {
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
$this->errors->add('attachments', $msg);
Log::error($msg);
$result = true;
}
return $result;
}
}