Expand and refactor factories.

This commit is contained in:
James Cole
2018-02-19 19:44:46 +01:00
parent 5bb7530642
commit b3fe24b713
11 changed files with 549 additions and 241 deletions

View File

@@ -28,6 +28,8 @@ use FireflyIII\Models\AccountType;
use FireflyIII\User; use FireflyIII\User;
/** /**
* Factory to create or return accounts.
*
* Class AccountFactory * Class AccountFactory
*/ */
class AccountFactory class AccountFactory
@@ -35,14 +37,6 @@ class AccountFactory
/** @var User */ /** @var User */
private $user; private $user;
/**
* TagFactory constructor.
*/
public function __construct()
{
}
/** /**
* @param array $data * @param array $data
* *
@@ -71,7 +65,7 @@ class AccountFactory
} }
} }
$newAccount = $this->create( return $this->create(
[ [
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'name' => $accountName, 'name' => $accountName,
@@ -81,7 +75,6 @@ class AccountFactory
'active' => true, 'active' => true,
] ]
); );
return $newAccount;
} }
/** /**

View File

@@ -0,0 +1,89 @@
<?php
/**
* BillFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\User;
/**
* Class BillFactory
*/
class BillFactory
{
/** @var BillRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* BillFactory constructor.
*/
public function __construct()
{
$this->repository = app(BillRepositoryInterface::class);
}
/**
* @param int|null $billId
* @param null|string $billName
*
* @return Bill|null
*/
public function find(?int $billId, ?string $billName): ?Bill
{
$billId = intval($billId);
$billName = strval($billName);
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $this->repository->find($billId);
if (!is_null($bill)) {
return $bill;
}
}
// then find by name:
if (strlen($billName) > 0) {
$bill = $this->repository->findByName($billName);
if (!is_null($bill)) {
return $bill;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* BudgetFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\User;
/**
* Class BudgetFactory
*/
class BudgetFactory
{
/** @var BudgetRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* BudgetFactory constructor.
*/
public function __construct()
{
$this->repository = app(BudgetRepositoryInterface::class);
}
/**
* @param int|null $budgetId
* @param null|string $budgetName
*
* @return Budget|null
*/
public function find(?int $budgetId, ?string $budgetName): ?Budget
{
$budgetId = intval($budgetId);
$budgetName = strval($budgetName);
if (strlen($budgetName) === 0 && $budgetId === 0) {
return null;
}
// first by ID:
if ($budgetId > 0) {
$budget = $this->repository->findNull($budgetId);
if (!is_null($budget)) {
return $budget;
}
}
if (strlen($budgetName) > 0) {
$budget = $this->repository->findByName($budgetName);
if (!is_null($budget)) {
return $budget;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* CategoryFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Category;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\User;
use Log;
/**
* Class CategoryFactory
*/
class CategoryFactory
{
/** @var CategoryRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* CategoryFactory constructor.
*/
public function __construct()
{
$this->repository = app(CategoryRepositoryInterface::class);
}
/**
* @param int|null $categoryId
* @param null|string $categoryName
*
* @return Category|null
*/
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{
$categoryId = intval($categoryId);
$categoryName = strval($categoryName);
Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));
if (strlen($categoryName) === 0 && $categoryId === 0) {
return null;
}
// first by ID:
if ($categoryId > 0) {
$category = $this->repository->findNull($categoryId);
if (!is_null($category)) {
return $category;
}
}
if (strlen($categoryName) > 0) {
$category = $this->repository->findByName($categoryName);
if (!is_null($category)) {
return $category;
}
return Category::create(
[
'user_id' => $this->user->id,
'name' => $categoryName,
]
);
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
}

View File

@@ -26,11 +26,13 @@ namespace FireflyIII\Factory;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log; use Log;
/** /**
* Create piggy bank events.
*
* Class PiggyBankEventFactory * Class PiggyBankEventFactory
*/ */
class PiggyBankEventFactory class PiggyBankEventFactory
@@ -46,21 +48,18 @@ class PiggyBankEventFactory
if (is_null($piggyBank)) { if (is_null($piggyBank)) {
return null; return null;
} }
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
/** @var PiggyBankRepositoryInterface $piggyRepos */
$piggyRepos = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
$piggyRepos->setUser($journal->user);
// is a transfer? // is a transfer?
if (!$repository->isTransfer($journal)) { if (!TransactionType::TRANSFER === $journal->transactionType->type) {
Log::info(sprintf('Will not connect %s #%d to a piggy bank.', $journal->transactionType->type, $journal->id)); Log::info(sprintf('Will not connect %s #%d to a piggy bank.', $journal->transactionType->type, $journal->id));
return null; return null;
} }
/** @var PiggyBankRepositoryInterface $piggyRepos */
$piggyRepos = app(PiggyBankRepositoryInterface::class);
$piggyRepos->setUser($journal->user);
// repetition exists? // repetition exists?
$repetition = $piggyRepos->getRepetition($piggyBank, $journal->date); $repetition = $piggyRepos->getRepetition($piggyBank, $journal->date);
if (null === $repetition->id) { if (null === $repetition->id) {

View File

@@ -0,0 +1,94 @@
<?php
/**
* PiggyBankFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\User;
/**
* Class PiggyBankFactory
*/
class PiggyBankFactory
{
/** @var PiggyBankRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* PiggyBankFactory constructor.
*/
public function __construct()
{
$this->repository = app(PiggyBankRepositoryInterface::class);
}
/**
* @param int|null $piggyBankId
* @param null|string $piggyBankName
*
* @return PiggyBank|null
*/
public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank
{
$piggyBankId = intval($piggyBankId);
$piggyBankName = strval($piggyBankName);
if (strlen($piggyBankName) === 0 && $piggyBankId === 0) {
return null;
}
// first find by ID:
if ($piggyBankId > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->repository->find($piggyBankId);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
// then find by name:
if (strlen($piggyBankName) > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->repository->findByName($piggyBankName);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
}

View File

@@ -37,14 +37,6 @@ class TagFactory
/** @var User */ /** @var User */
private $user; private $user;
/**
* TagFactory constructor.
*/
public function __construct()
{
}
/** /**
* @param array $data * @param array $data
* *
@@ -83,6 +75,7 @@ class TagFactory
return $object; return $object;
} }
} }
$newTag = $this->create( $newTag = $this->create(
[ [
'tag' => $tag, 'tag' => $tag,

View File

@@ -0,0 +1,67 @@
<?php
/**
* TransactionCurrencyFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\TransactionCurrency;
/**
* Class TransactionCurrencyFactory
*/
class TransactionCurrencyFactory
{
/**
* @param int|null $currencyId
* @param null|string $currencyCode
*
* @return TransactionCurrency|null
*/
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
$currencyCode = strval($currencyCode);
$currencyId = intval($currencyId);
if (strlen($currencyCode) === 0 && intval($currencyId) === 0) {
return null;
}
// first by ID:
if ($currencyId > 0) {
$currency = TransactionCurrency::find($currencyId);
if (!is_null($currency)) {
return $currency;
}
}
// then by code:
if (strlen($currencyCode) > 0) {
$currency = TransactionCurrency::whereCode($currencyCode)->first();
if (!is_null($currency)) {
return $currency;
}
}
return null;
}
}

View File

@@ -34,10 +34,6 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -48,14 +44,6 @@ class TransactionFactory
{ {
/** @var AccountRepositoryInterface */ /** @var AccountRepositoryInterface */
private $accountRepository; private $accountRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var JournalRepositoryInterface */
private $repository;
/** @var User */ /** @var User */
private $user; private $user;
@@ -64,11 +52,7 @@ class TransactionFactory
*/ */
public function __construct() public function __construct()
{ {
$this->repository = app(JournalRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
} }
/** /**
@@ -78,7 +62,8 @@ class TransactionFactory
*/ */
public function create(array $data): Transaction public function create(array $data): Transaction
{ {
$values = [ return Transaction::create(
[
'reconciled' => $data['reconciled'], 'reconciled' => $data['reconciled'],
'account_id' => $data['account']->id, 'account_id' => $data['account']->id,
'transaction_journal_id' => $data['transaction_journal']->id, 'transaction_journal_id' => $data['transaction_journal']->id,
@@ -88,9 +73,8 @@ class TransactionFactory
'foreign_amount' => $data['foreign_amount'], 'foreign_amount' => $data['foreign_amount'],
'foreign_currency_id' => null, 'foreign_currency_id' => null,
'identifier' => $data['identifier'], 'identifier' => $data['identifier'],
]; ]
$transaction = $this->repository->storeBasicTransaction($values); );
return $transaction;
} }
/** /**
@@ -141,6 +125,7 @@ class TransactionFactory
'identifier' => $data['identifier'], 'identifier' => $data['identifier'],
] ]
); );
// set foreign currency // set foreign currency
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']); $foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
$this->setForeignCurrency($source, $foreign); $this->setForeignCurrency($source, $foreign);
@@ -171,11 +156,7 @@ class TransactionFactory
public function setUser(User $user) public function setUser(User $user)
{ {
$this->user = $user; $this->user = $user;
$this->repository->setUser($user);
$this->accountRepository->setUser($user); $this->accountRepository->setUser($user);
$this->budgetRepository->setUser($user);
$this->categoryRepository->setUser($user);
$this->currencyRepository->setUser($user);
} }
/** /**
@@ -244,6 +225,7 @@ class TransactionFactory
/** @var AccountFactory $factory */ /** @var AccountFactory $factory */
$factory = app(AccountFactory::class); $factory = app(AccountFactory::class);
$factory->setUser($this->user); $factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::EXPENSE); return $factory->findOrCreate($accountName, AccountType::EXPENSE);
} }
@@ -260,6 +242,7 @@ class TransactionFactory
/** @var AccountFactory $factory */ /** @var AccountFactory $factory */
$factory = app(AccountFactory::class); $factory = app(AccountFactory::class);
$factory->setUser($this->user); $factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::REVENUE); return $factory->findOrCreate($accountName, AccountType::REVENUE);
} }
@@ -280,28 +263,11 @@ class TransactionFactory
*/ */
protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget
{ {
$budgetId = intval($budgetId); /** @var BudgetFactory $factory */
$budgetName = strval($budgetName); $factory = app(BudgetFactory::class);
if (strlen($budgetName) === 0 && $budgetId === 0) { $factory->setUser($this->user);
return null;
}
// first by ID: return $factory->find($budgetId, $budgetName);
if ($budgetId > 0) {
$budget = $this->budgetRepository->findNull($budgetId);
if (!is_null($budget)) {
return $budget;
}
}
if (strlen($budgetName) > 0) {
$budget = $this->budgetRepository->findByName($budgetName);
if (!is_null($budget)) {
return $budget;
}
}
return null;
} }
/** /**
@@ -312,30 +278,11 @@ class TransactionFactory
*/ */
protected function findCategory(?int $categoryId, ?string $categoryName): ?Category protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
{ {
$categoryId = intval($categoryId); /** @var CategoryFactory $factory */
$categoryName = strval($categoryName); $factory = app(CategoryFactory::class);
$factory->setUser($this->user);
if (strlen($categoryName) === 0 && $categoryId === 0) { return $factory->findOrCreate($categoryId, $categoryName);
return null;
}
// first by ID:
if ($categoryId > 0) {
$category = $this->categoryRepository->findNull($categoryId);
if (!is_null($category)) {
return $category;
}
}
if (strlen($categoryName) > 0) {
$category = $this->categoryRepository->findByName($categoryName);
if (!is_null($category)) {
return $category;
}
// create it?
die('create category');
}
return null;
} }
/** /**
@@ -346,29 +293,9 @@ class TransactionFactory
*/ */
protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{ {
$currencyCode = strval($currencyCode); $factory = app(TransactionCurrencyFactory::class);
$currencyId = intval($currencyId);
if (strlen($currencyCode) === 0 && intval($currencyId) === 0) { return $factory->find($currencyId, $currencyCode);
return null;
}
// first by ID:
if ($currencyId > 0) {
$currency = $this->currencyRepository->findNull($currencyId);
if (!is_null($currency)) {
return $currency;
}
}
// then by code:
if (strlen($currencyCode) > 0) {
$currency = $this->currencyRepository->findByCodeNull($currencyCode);
if (!is_null($currency)) {
return $currency;
}
}
return null;
} }
/** /**

View File

@@ -24,15 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Factory; namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -40,29 +34,9 @@ use FireflyIII\User;
*/ */
class TransactionJournalFactory class TransactionJournalFactory
{ {
/** @var BillRepositoryInterface */
private $billRepository;
/** @var PiggyBankRepositoryInterface */
private $piggyRepository;
/** @var JournalRepositoryInterface */
private $repository;
/** @var TransactionTypeRepositoryInterface */
private $ttRepository;
/** @var User */ /** @var User */
private $user; private $user;
/**
* TransactionJournalFactory constructor.
*/
public function __construct()
{
$this->repository = app(JournalRepositoryInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
$this->piggyRepository = app(PiggyBankRepositoryInterface::class);
$this->ttRepository = app(TransactionTypeRepositoryInterface::class);
}
/** /**
* Create a new transaction journal and associated transactions. * Create a new transaction journal and associated transactions.
* *
@@ -76,7 +50,8 @@ class TransactionJournalFactory
// store basic journal first. // store basic journal first.
$type = $this->findTransactionType($data['type']); $type = $this->findTransactionType($data['type']);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user); $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
$values = [ $journal = TransactionJournal::create(
[
'user_id' => $data['user'], 'user_id' => $data['user'],
'transaction_type_id' => $type->id, 'transaction_type_id' => $type->id,
'bill_id' => null, 'bill_id' => null,
@@ -86,9 +61,8 @@ class TransactionJournalFactory
'order' => 0, 'order' => 0,
'tag_count' => 0, 'tag_count' => 0,
'completed' => 0, 'completed' => 0,
]; ]
);
$journal = $this->repository->storeBasic($values);
// store basic transactions: // store basic transactions:
$factory = app(TransactionFactory::class); $factory = app(TransactionFactory::class);
@@ -98,7 +72,8 @@ class TransactionJournalFactory
foreach ($data['transactions'] as $trData) { foreach ($data['transactions'] as $trData) {
$factory->createPair($journal, $trData); $factory->createPair($journal, $trData);
} }
$this->repository->markCompleted($journal); $journal->completed = true;
$journal->save();
// link bill: // link bill:
$this->connectBill($journal, $data); $this->connectBill($journal, $data);
@@ -132,9 +107,6 @@ class TransactionJournalFactory
public function setUser(User $user): void public function setUser(User $user): void
{ {
$this->user = $user; $this->user = $user;
$this->repository->setUser($user);
$this->billRepository->setUser($user);
$this->piggyRepository->setUser($user);
} }
/** /**
@@ -145,7 +117,11 @@ class TransactionJournalFactory
*/ */
protected function connectBill(TransactionJournal $journal, array $data): void protected function connectBill(TransactionJournal $journal, array $data): void
{ {
$bill = $this->findBill($data['bill_id'], $data['bill_name']); /** @var BillFactory $factory */
$factory = app(BillFactory::class);
$factory->setUser($this->user);
$bill = $factory->find($data['bill_id'], $data['bill_name']);
if (!is_null($bill)) { if (!is_null($bill)) {
$journal->bill_id = $bill->id; $journal->bill_id = $bill->id;
$journal->save(); $journal->save();
@@ -158,7 +134,11 @@ class TransactionJournalFactory
*/ */
protected function connectPiggyBank(TransactionJournal $journal, array $data): void protected function connectPiggyBank(TransactionJournal $journal, array $data): void
{ {
$piggyBank = $this->findPiggyBank($data['piggy_bank_id'], $data['piggy_bank_name']); /** @var PiggyBankFactory $factory */
$factory = app(PiggyBankFactory::class);
$factory->setUser($this->user);
$piggyBank = $factory->find($data['piggy_bank_id'], $data['piggy_bank_name']);
if (!is_null($piggyBank)) { if (!is_null($piggyBank)) {
/** @var PiggyBankEventFactory $factory */ /** @var PiggyBankEventFactory $factory */
$factory = app(PiggyBankEventFactory::class); $factory = app(PiggyBankEventFactory::class);
@@ -180,74 +160,6 @@ class TransactionJournalFactory
} }
} }
/**
* Find the given bill based on the ID or the name. ID takes precedence over the name.
*
* @param int $billId
* @param string $billName
*
* @return Bill|null
*/
protected function findBill(int $billId, string $billName): ?Bill
{
if (strlen($billName) === 0 && $billId === 0) {
return null;
}
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $this->billRepository->find($billId);
if (!is_null($bill)) {
return $bill;
}
}
// then find by name:
if (strlen($billName) > 0) {
$bill = $this->billRepository->findByName($billName);
if (!is_null($bill)) {
return $bill;
}
}
return null;
}
/**
* Find the given bill based on the ID or the name. ID takes precedence over the name.
*
* @param int $piggyBankId
* @param string $piggyBankName
*
* @return PiggyBank|null
*/
protected function findPiggyBank(int $piggyBankId, string $piggyBankName): ?PiggyBank
{
if (strlen($piggyBankName) === 0 && $piggyBankId === 0) {
return null;
}
// first find by ID:
if ($piggyBankId > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->piggyRepository->find($piggyBankId);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
// then find by name:
if (strlen($piggyBankName) > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->piggyRepository->findByName($piggyBankName);
if (!is_null($piggyBank)) {
return $piggyBank;
}
}
return null;
}
/** /**
* Get the transaction type. Since this is mandatory, will throw an exception when nothing comes up. Will always * Get the transaction type. Since this is mandatory, will throw an exception when nothing comes up. Will always
* use TransactionType repository. * use TransactionType repository.
@@ -259,7 +171,8 @@ class TransactionJournalFactory
*/ */
protected function findTransactionType(string $type): TransactionType protected function findTransactionType(string $type): TransactionType
{ {
$transactionType = $this->ttRepository->findByType($type); $factory = app(TransactionTypeFactory::class);
$transactionType = $factory->find($type);
if (is_null($transactionType)) { if (is_null($transactionType)) {
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type)); throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
} }

View File

@@ -0,0 +1,43 @@
<?php
/**
* TransactionTypeFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\TransactionType;
/**
* Class TransactionTypeFactory
*/
class TransactionTypeFactory
{
/**
* @param string $type
*
* @return TransactionType|null
*/
public function find(string $type): ?TransactionType
{
return TransactionType::whereType(ucfirst($type))->first();
}
}