mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various fixes and checks.
This commit is contained in:
@@ -23,8 +23,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Factory;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Attachment;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
|
||||
@@ -33,6 +36,9 @@ use Log;
|
||||
*/
|
||||
class AttachmentFactory
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@@ -42,22 +48,34 @@ class AttachmentFactory
|
||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
||||
}
|
||||
}
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Attachment|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(array $data): ?Attachment
|
||||
{
|
||||
// append if necessary.
|
||||
$model = false === strpos('FireflyIII', $data['model']) ? 'FireflyIII\\Models\\' . $data['model'] : $data['model'];
|
||||
|
||||
if (Transaction::class === $model) {
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $this->user->transactions()->find((int)$data['model_id']);
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException('Unexpectedly could not find transaction');
|
||||
}
|
||||
$data['model_id'] = $transaction->transaction_journal_id;
|
||||
$model = TransactionJournal::class;
|
||||
}
|
||||
|
||||
// create attachment:
|
||||
$attachment = Attachment::create(
|
||||
[
|
||||
'user_id' => $this->user->id,
|
||||
'attachable_id' => $data['model_id'],
|
||||
'attachable_type' => $data['model'],
|
||||
'attachable_type' => $model,
|
||||
'md5' => '',
|
||||
'filename' => $data['filename'],
|
||||
'title' => '' === $data['title'] ? null : $data['title'],
|
||||
|
Reference in New Issue
Block a user