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

102 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* TransactionGroupFactory.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* 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.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\DuplicateTransactionException;
2021-03-21 11:06:08 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\User;
2020-02-22 11:05:16 +01:00
use Log;
/**
* Class TransactionGroupFactory
2019-08-30 07:28:48 +02:00
*
* @codeCoverageIgnore
*/
class TransactionGroupFactory
{
/** @var TransactionJournalFactory */
private $journalFactory;
/** @var User The user */
private $user;
2021-03-28 11:46:23 +02:00
/**
* TransactionGroupFactory constructor.
*/
public function __construct()
{
$this->journalFactory = app(TransactionJournalFactory::class);
}
/**
* Store a new transaction journal.
*
* @param array $data
*
* @return TransactionGroup
2021-03-21 09:15:40 +01:00
* @throws DuplicateTransactionException
2021-05-24 08:50:17 +02:00
* @throws FireflyException
*/
public function create(array $data): TransactionGroup
{
2020-10-04 17:11:59 +02:00
Log::debug('Now in TransactionGroupFactory::create()');
$this->journalFactory->setUser($this->user);
2019-10-26 16:21:12 +02:00
$this->journalFactory->setErrorOnHash($data['error_if_duplicate_hash'] ?? false);
2020-02-22 11:05:16 +01:00
try {
$collection = $this->journalFactory->create($data);
} catch (DuplicateTransactionException $e) {
2020-02-22 11:05:16 +01:00
Log::warning('GroupFactory::create() caught journalFactory::create() with a duplicate!');
2021-04-07 07:28:43 +02:00
throw new DuplicateTransactionException($e->getMessage(), 0, $e);
2020-02-22 11:05:16 +01:00
}
$title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title;
2019-08-30 07:28:48 +02:00
if (null !== $title) {
2019-09-21 07:33:13 +02:00
$title = substr($title, 0, 1000);
2019-08-30 07:28:48 +02:00
}
2021-03-28 11:46:23 +02:00
if (0 === $collection->count()) {
2021-03-21 11:06:08 +01:00
throw new FireflyException('Created zero transaction journals.');
}
2019-08-30 07:28:48 +02:00
$group = new TransactionGroup;
2019-08-16 17:54:38 +02:00
$group->user()->associate($this->user);
$group->title = $title;
$group->save();
$group->transactionJournals()->saveMany($collection);
return $group;
}
/**
* Set the user.
*
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
2019-08-17 12:09:03 +02:00
}