Files
firefly-iii/app/Factory/AttachmentFactory.php

97 lines
3.0 KiB
PHP
Raw Normal View History

2018-06-24 06:51:22 +02:00
<?php
/**
* AttachmentFactory.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 06:51:22 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 06:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-06-24 06:51:22 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 06:51:22 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-06-24 06:51:22 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-24 06:51:22 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
2018-12-21 10:11:18 +01:00
use FireflyIII\Exceptions\FireflyException;
2018-06-24 06:51:22 +02:00
use FireflyIII\Models\Attachment;
use FireflyIII\Models\Note;
2018-12-21 10:11:18 +01:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2018-06-24 06:51:22 +02:00
use FireflyIII\User;
/**
* Class AttachmentFactory
*/
class AttachmentFactory
{
2020-08-22 12:24:01 +02:00
private User $user;
2018-06-24 06:51:22 +02:00
/**
* @param array $data
*
2018-12-21 10:11:18 +01:00
* @throws FireflyException
* @return Attachment|null
2018-06-24 06:51:22 +02:00
*/
public function create(array $data): ?Attachment
{
2018-12-21 10:11:18 +01:00
// append if necessary.
2021-03-13 14:33:48 +01:00
$model = false === strpos($data['attachable_type'], 'FireflyIII') ? sprintf('FireflyIII\\Models\\%s', $data['attachable_type']) : $data['attachable_type'];
2018-12-21 10:11:18 +01:00
// get journal instead of transaction.
2018-12-21 10:11:18 +01:00
if (Transaction::class === $model) {
/** @var Transaction $transaction */
2021-03-13 14:33:48 +01:00
$transaction = $this->user->transactions()->find((int) $data['attachable_id']);
2018-12-21 10:11:18 +01:00
if (null === $transaction) {
throw new FireflyException('Unexpectedly could not find transaction'); // @codeCoverageIgnore
2018-12-21 10:11:18 +01:00
}
2021-03-13 14:33:48 +01:00
$data['attachable_id'] = $transaction->transaction_journal_id;
2018-12-21 10:11:18 +01:00
$model = TransactionJournal::class;
}
2018-06-24 06:51:22 +02:00
// create attachment:
$attachment = Attachment::create(
[
'user_id' => $this->user->id,
2021-03-13 14:33:48 +01:00
'attachable_id' => $data['attachable_id'],
2018-12-21 10:11:18 +01:00
'attachable_type' => $model,
2018-06-24 06:51:22 +02:00
'md5' => '',
'filename' => $data['filename'],
'title' => '' === $data['title'] ? null : $data['title'],
'description' => null,
'mime' => '',
'size' => 0,
'uploaded' => 0,
]
);
$notes = (string) ($data['notes'] ?? '');
2018-06-24 06:51:22 +02:00
if ('' !== $notes) {
$note = new Note;
$note->noteable()->associate($attachment);
$note->text = $notes;
$note->save();
}
return $attachment;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}