Skip to content

Firefly III: a personal finances manager

Expand and refactor factories.

James Coleb3fe24b7131d
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 6e543fbb74..e618b7fd99 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -28,6 +28,8 @@ use FireflyIII\Models\AccountType;
use FireflyIII\User;
/**
+ * Factory to create or return accounts.
+ *
* Class AccountFactory
*/
class AccountFactory
@@ -35,14 +37,6 @@ class AccountFactory
/** @var User */
private $user;
- /**
- * TagFactory constructor.
- */
- public function __construct()
- {
-
- }
-
/**
* @param array $data
*
@@ -71,7 +65,7 @@ class AccountFactory
}
}
- $newAccount = $this->create(
+ return $this->create(
[
'user_id' => $this->user->id,
'name' => $accountName,
@@ -81,7 +75,6 @@ class AccountFactory
'active' => true,
]
);
- return $newAccount;
}
/**
diff --git a/app/Factory/BillFactory.php b/app/Factory/BillFactory.php
new file mode 100644
index 0000000000..386b5c1173
--- /dev/null
+++ b/app/Factory/BillFactory.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * BillFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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);
+
+ }
+
+}
\ No newline at end of file
diff --git a/app/Factory/BudgetFactory.php b/app/Factory/BudgetFactory.php
new file mode 100644
index 0000000000..bffde7d086
--- /dev/null
+++ b/app/Factory/BudgetFactory.php
@@ -0,0 +1,90 @@
+<?php
+/**
+ * BudgetFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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);
+ }
+
+}
\ No newline at end of file
diff --git a/app/Factory/CategoryFactory.php b/app/Factory/CategoryFactory.php
new file mode 100644
index 0000000000..eec437cab1
--- /dev/null
+++ b/app/Factory/CategoryFactory.php
@@ -0,0 +1,100 @@
+<?php
+/**
+ * CategoryFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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);
+ }
+
+}
\ No newline at end of file
diff --git a/app/Factory/PiggyBankEventFactory.php b/app/Factory/PiggyBankEventFactory.php
index 9ce51d1b47..a7dcbec5f1 100644
--- a/app/Factory/PiggyBankEventFactory.php
+++ b/app/Factory/PiggyBankEventFactory.php
@@ -26,11 +26,13 @@ namespace FireflyIII\Factory;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\TransactionJournal;
-use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
+use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
/**
+ * Create piggy bank events.
+ *
* Class PiggyBankEventFactory
*/
class PiggyBankEventFactory
@@ -46,21 +48,18 @@ class PiggyBankEventFactory
if (is_null($piggyBank)) {
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?
- 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));
return null;
}
+ /** @var PiggyBankRepositoryInterface $piggyRepos */
+ $piggyRepos = app(PiggyBankRepositoryInterface::class);
+ $piggyRepos->setUser($journal->user);
+
// repetition exists?
$repetition = $piggyRepos->getRepetition($piggyBank, $journal->date);
if (null === $repetition->id) {
diff --git a/app/Factory/PiggyBankFactory.php b/app/Factory/PiggyBankFactory.php
new file mode 100644
index 0000000000..163c48ee6c
--- /dev/null
+++ b/app/Factory/PiggyBankFactory.php
@@ -0,0 +1,94 @@
+<?php
+/**
+ * PiggyBankFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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);
+
+ }
+
+}
\ No newline at end of file
diff --git a/app/Factory/TagFactory.php b/app/Factory/TagFactory.php
index e1b9adbb62..0fc74c2a0e 100644
--- a/app/Factory/TagFactory.php
+++ b/app/Factory/TagFactory.php
@@ -37,14 +37,6 @@ class TagFactory
/** @var User */
private $user;
- /**
- * TagFactory constructor.
- */
- public function __construct()
- {
-
- }
-
/**
* @param array $data
*
@@ -83,6 +75,7 @@ class TagFactory
return $object;
}
}
+
$newTag = $this->create(
[
'tag' => $tag,
diff --git a/app/Factory/TransactionCurrencyFactory.php b/app/Factory/TransactionCurrencyFactory.php
new file mode 100644
index 0000000000..0c66c2942a
--- /dev/null
+++ b/app/Factory/TransactionCurrencyFactory.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * TransactionCurrencyFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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;
+ }
+
+
+}
\ No newline at end of file
diff --git a/app/Factory/TransactionFactory.php b/app/Factory/TransactionFactory.php
index 1a3d98b019..35bd0338ca 100644
--- a/app/Factory/TransactionFactory.php
+++ b/app/Factory/TransactionFactory.php
@@ -34,10 +34,6 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
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 Illuminate\Support\Collection;
@@ -48,14 +44,6 @@ class TransactionFactory
{
/** @var AccountRepositoryInterface */
private $accountRepository;
- /** @var BudgetRepositoryInterface */
- private $budgetRepository;
- /** @var CategoryRepositoryInterface */
- private $categoryRepository;
- /** @var CurrencyRepositoryInterface */
- private $currencyRepository;
- /** @var JournalRepositoryInterface */
- private $repository;
/** @var User */
private $user;
@@ -64,11 +52,7 @@ class TransactionFactory
*/
public function __construct()
{
- $this->repository = app(JournalRepositoryInterface::class);
- $this->accountRepository = app(AccountRepositoryInterface::class);
- $this->budgetRepository = app(BudgetRepositoryInterface::class);
- $this->categoryRepository = app(CategoryRepositoryInterface::class);
- $this->currencyRepository = app(CurrencyRepositoryInterface::class);
+ $this->accountRepository = app(AccountRepositoryInterface::class);
}
/**
@@ -78,19 +62,19 @@ class TransactionFactory
*/
public function create(array $data): Transaction
{
- $values = [
- 'reconciled' => $data['reconciled'],
- 'account_id' => $data['account']->id,
- 'transaction_journal_id' => $data['transaction_journal']->id,
- 'description' => $data['description'],
- 'transaction_currency_id' => $data['currency']->id,
- 'amount' => $data['amount'],
- 'foreign_amount' => $data['foreign_amount'],
- 'foreign_currency_id' => null,
- 'identifier' => $data['identifier'],
- ];
- $transaction = $this->repository->storeBasicTransaction($values);
- return $transaction;
+ return Transaction::create(
+ [
+ 'reconciled' => $data['reconciled'],
+ 'account_id' => $data['account']->id,
+ 'transaction_journal_id' => $data['transaction_journal']->id,
+ 'description' => $data['description'],
+ 'transaction_currency_id' => $data['currency']->id,
+ 'amount' => $data['amount'],
+ 'foreign_amount' => $data['foreign_amount'],
+ 'foreign_currency_id' => null,
+ 'identifier' => $data['identifier'],
+ ]
+ );
}
/**
@@ -141,6 +125,7 @@ class TransactionFactory
'identifier' => $data['identifier'],
]
);
+
// set foreign currency
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
$this->setForeignCurrency($source, $foreign);
@@ -171,11 +156,7 @@ class TransactionFactory
public function setUser(User $user)
{
$this->user = $user;
- $this->repository->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 */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
+
return $factory->findOrCreate($accountName, AccountType::EXPENSE);
}
@@ -260,6 +242,7 @@ class TransactionFactory
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
+
return $factory->findOrCreate($accountName, AccountType::REVENUE);
}
@@ -280,28 +263,11 @@ class TransactionFactory
*/
protected function findBudget(?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->budgetRepository->findNull($budgetId);
- if (!is_null($budget)) {
- return $budget;
- }
- }
-
- if (strlen($budgetName) > 0) {
- $budget = $this->budgetRepository->findByName($budgetName);
- if (!is_null($budget)) {
- return $budget;
- }
- }
+ /** @var BudgetFactory $factory */
+ $factory = app(BudgetFactory::class);
+ $factory->setUser($this->user);
- return null;
+ return $factory->find($budgetId, $budgetName);
}
/**
@@ -312,30 +278,11 @@ class TransactionFactory
*/
protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
{
- $categoryId = intval($categoryId);
- $categoryName = strval($categoryName);
+ /** @var CategoryFactory $factory */
+ $factory = app(CategoryFactory::class);
+ $factory->setUser($this->user);
- if (strlen($categoryName) === 0 && $categoryId === 0) {
- 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;
+ return $factory->findOrCreate($categoryId, $categoryName);
}
/**
@@ -346,29 +293,9 @@ class TransactionFactory
*/
protected function findCurrency(?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 = $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;
- }
- }
+ $factory = app(TransactionCurrencyFactory::class);
- return null;
+ return $factory->find($currencyId, $currencyCode);
}
/**
diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php
index 1a5d7b4c31..201695d018 100644
--- a/app/Factory/TransactionJournalFactory.php
+++ b/app/Factory/TransactionJournalFactory.php
@@ -24,15 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
-use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
-use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionJournal;
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;
/**
@@ -40,29 +34,9 @@ use FireflyIII\User;
*/
class TransactionJournalFactory
{
- /** @var BillRepositoryInterface */
- private $billRepository;
- /** @var PiggyBankRepositoryInterface */
- private $piggyRepository;
- /** @var JournalRepositoryInterface */
- private $repository;
- /** @var TransactionTypeRepositoryInterface */
- private $ttRepository;
/** @var 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.
*
@@ -76,19 +50,19 @@ class TransactionJournalFactory
// store basic journal first.
$type = $this->findTransactionType($data['type']);
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
- $values = [
- 'user_id' => $data['user'],
- 'transaction_type_id' => $type->id,
- 'bill_id' => null,
- 'transaction_currency_id' => $defaultCurrency->id,
- 'description' => $data['description'],
- 'date' => $data['date']->format('Y-m-d'),
- 'order' => 0,
- 'tag_count' => 0,
- 'completed' => 0,
- ];
-
- $journal = $this->repository->storeBasic($values);
+ $journal = TransactionJournal::create(
+ [
+ 'user_id' => $data['user'],
+ 'transaction_type_id' => $type->id,
+ 'bill_id' => null,
+ 'transaction_currency_id' => $defaultCurrency->id,
+ 'description' => $data['description'],
+ 'date' => $data['date']->format('Y-m-d'),
+ 'order' => 0,
+ 'tag_count' => 0,
+ 'completed' => 0,
+ ]
+ );
// store basic transactions:
$factory = app(TransactionFactory::class);
@@ -98,7 +72,8 @@ class TransactionJournalFactory
foreach ($data['transactions'] as $trData) {
$factory->createPair($journal, $trData);
}
- $this->repository->markCompleted($journal);
+ $journal->completed = true;
+ $journal->save();
// link bill:
$this->connectBill($journal, $data);
@@ -132,9 +107,6 @@ class TransactionJournalFactory
public function setUser(User $user): void
{
$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
{
- $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)) {
$journal->bill_id = $bill->id;
$journal->save();
@@ -158,7 +134,11 @@ class TransactionJournalFactory
*/
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)) {
/** @var PiggyBankEventFactory $factory */
$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
* use TransactionType repository.
@@ -259,7 +171,8 @@ class TransactionJournalFactory
*/
protected function findTransactionType(string $type): TransactionType
{
- $transactionType = $this->ttRepository->findByType($type);
+ $factory = app(TransactionTypeFactory::class);
+ $transactionType = $factory->find($type);
if (is_null($transactionType)) {
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
}
diff --git a/app/Factory/TransactionTypeFactory.php b/app/Factory/TransactionTypeFactory.php
new file mode 100644
index 0000000000..db64162827
--- /dev/null
+++ b/app/Factory/TransactionTypeFactory.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * TransactionTypeFactory.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * 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();
+ }
+
+}
\ No newline at end of file