Skip to content

Firefly III: a personal finances manager

Avoid using model methods and use repository instead

James Cole1645490f5c3d
diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index eb0a2b89d0..b9ff714633 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -281,6 +281,7 @@ class AccountController extends Controller
* @param Account $account
* @param Carbon|null $start
* @param Carbon|null $end
+ *
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
*
* @throws FireflyException
@@ -303,11 +304,11 @@ class AccountController extends Controller
throw new FireflyException('End is after start!'); // @codeCoverageIgnore
}
- $today = new Carbon;
+ $today = new Carbon;
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
- $currencyId = intval($account->getMeta('currency_id'));
+ $currencyId = intval($this->repository->getMetaValue($account, 'currency_id'));
$currency = $this->currencyRepos->findNull($currencyId);
if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
@@ -327,7 +328,7 @@ class AccountController extends Controller
return view(
'accounts.show',
- compact('account', 'currency','today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
+ compact('account', 'currency', 'today', 'periods', 'subTitleIcon', 'transactions', 'subTitle', 'start', 'end', 'chartUri')
);
}
@@ -411,6 +412,7 @@ class AccountController extends Controller
* @param Account $account the account involved
*
* @param Carbon|null $date
+ *
* @return Collection
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index e0343210f5..14b4e67ac4 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -125,6 +125,23 @@ class AccountRepository implements AccountRepositoryInterface
return $account->notes()->first();
}
+ /**
+ * Get note text or null.
+ *
+ * @param Account $account
+ *
+ * @return null|string
+ */
+ public function getNoteText(Account $account): ?string
+ {
+ $note = $account->notes()->first();
+ if (is_null($note)) {
+ return null;
+ }
+
+ return $note->text;
+ }
+
/**
* Returns the amount of the opening balance for this account.
*
@@ -285,5 +302,4 @@ class AccountRepository implements AccountRepositoryInterface
return $journal;
}
-
}
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index e81a72bf2b..e7e00f60dd 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -145,6 +145,15 @@ interface AccountRepositoryInterface
*/
public function getNote(Account $account): ?Note;
+ /**
+ * Get note text or null.
+ *
+ * @param Account $account
+ *
+ * @return null|string
+ */
+ public function getNoteText(Account $account): ?string;
+
/**
* Returns the amount of the opening balance for this account.
*
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index 3a3b0d2444..e5041c0390 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Journal;
use Carbon\Carbon;
use Exception;
use FireflyIII\Factory\TransactionJournalFactory;
+use FireflyIII\Factory\TransactionJournalMetaFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Note;
@@ -384,7 +385,7 @@ class JournalRepository implements JournalRepositoryInterface
$cache->addProperty($field);
if ($cache->has()) {
- return $cache->get(); // @codeCoverageIgnore
+ return new Carbon($cache->get()); // @codeCoverageIgnore
}
$entry = $journal->transactionJournalMeta()->where('name', $field)->first();
@@ -392,7 +393,7 @@ class JournalRepository implements JournalRepositoryInterface
return null;
}
$value = new Carbon($entry->data);
- $cache->store($value);
+ $cache->store($entry->data);
return $value;
}
@@ -597,6 +598,52 @@ class JournalRepository implements JournalRepositoryInterface
return false;
}
+ /**
+ * Set meta field for journal that contains a date.
+ *
+ * @param TransactionJournal $journal
+ * @param string $name
+ * @param Carbon $date
+ *
+ * @return void
+ */
+ public function setMetaDate(TransactionJournal $journal, string $name, Carbon $date): void
+ {
+ /** @var TransactionJournalMetaFactory $factory */
+ $factory = app(TransactionJournalMetaFactory::class);
+ $factory->updateOrCreate(
+ [
+ 'data' => $date,
+ 'journal' => $journal,
+ 'name' => $name,
+ ]
+ );
+
+ return;
+ }
+
+ /**
+ * Set meta field for journal that contains string.
+ *
+ * @param TransactionJournal $journal
+ * @param string $name
+ * @param string $value
+ */
+ public function setMetaString(TransactionJournal $journal, string $name, string $value): void
+ {
+ /** @var TransactionJournalMetaFactory $factory */
+ $factory = app(TransactionJournalMetaFactory::class);
+ $factory->updateOrCreate(
+ [
+ 'data' => $value,
+ 'journal' => $journal,
+ 'name' => $name,
+ ]
+ );
+
+ return;
+ }
+
/**
* @param TransactionJournal $journal
* @param int $order
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 393a38d8ed..cefcc53a31 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -262,6 +262,26 @@ interface JournalRepositoryInterface
*/
public function reconcileById(int $transactionId): bool;
+ /**
+ * Set meta field for journal that contains a date.
+ *
+ * @param TransactionJournal $journal
+ * @param string $name
+ * @param Carbon $date
+ *
+ * @return void
+ */
+ public function setMetaDate(TransactionJournal $journal, string $name, Carbon $date): void;
+
+ /**
+ * Set meta field for journal that contains string.
+ *
+ * @param TransactionJournal $journal
+ * @param string $name
+ * @param string $value
+ */
+ public function setMetaString(TransactionJournal $journal, string $name, string $value): void;
+
/**
* @param TransactionJournal $journal
* @param int $order
diff --git a/app/Support/Twig/Extension/TransactionJournal.php b/app/Support/Twig/Extension/TransactionJournal.php
index 510dc581fe..6e8d9a9ede 100644
--- a/app/Support/Twig/Extension/TransactionJournal.php
+++ b/app/Support/Twig/Extension/TransactionJournal.php
@@ -66,6 +66,30 @@ class TransactionJournal extends Twig_Extension
return $result;
}
+ /**
+ * Return if journal HAS field.
+ *
+ * @param JournalModel $journal
+ * @param string $field
+ *
+ * @return bool
+ */
+ public function hasMetaField(JournalModel $journal, string $field): bool
+ {
+ // HIER BEN JE
+ /** @var JournalRepositoryInterface $repository */
+ $repository = app(JournalRepositoryInterface::class);
+ $result = $repository->getMetaField($journal, $field);
+ if (is_null($result)) {
+ return false;
+ }
+ if (strlen(strval($result)) === 0) {
+ return false;
+ }
+
+ return true;
+ }
+
/**
* @param JournalModel $journal
*
diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php
index 3e18a565b3..7bb8d7dea0 100644
--- a/app/Support/Twig/Journal.php
+++ b/app/Support/Twig/Journal.php
@@ -94,8 +94,9 @@ class Journal extends Twig_Extension
$this->getDestinationAccount(),
$this->journalBudgets(),
$this->journalCategories(),
- new Twig_SimpleFunction('getMetaField', [TransactionJournalExtension::class, 'getMetaField']),
- new Twig_SimpleFunction('getMetaDate', [TransactionJournalExtension::class, 'getMetaDate']),
+ new Twig_SimpleFunction('journalGetMetaField', [TransactionJournalExtension::class, 'getMetaField']),
+ new Twig_SimpleFunction('journalHasMeta', [TransactionJournalExtension::class, 'hasMetaField']),
+ new Twig_SimpleFunction('journalGetMetaDate', [TransactionJournalExtension::class, 'getMetaDate']),
];
return $functions;
diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php
index cd3f339433..d8a3fda122 100644
--- a/app/Transformers/AccountTransformer.php
+++ b/app/Transformers/AccountTransformer.php
@@ -28,7 +28,6 @@ use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
-use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
@@ -57,6 +56,9 @@ class AccountTransformer extends TransformerAbstract
/** @var ParameterBag */
protected $parameters;
+ /** @var AccountRepositoryInterface */
+ protected $repository;
+
/**
*
* AccountTransformer constructor.
@@ -67,6 +69,7 @@ class AccountTransformer extends TransformerAbstract
*/
public function __construct(ParameterBag $parameters)
{
+ $this->repository = app(AccountRepositoryInterface::class);
$this->parameters = $parameters;
}
@@ -140,12 +143,14 @@ class AccountTransformer extends TransformerAbstract
*/
public function transform(Account $account): array
{
+ $this->repository->setUser($account->user);
+
$type = $account->accountType->type;
- $role = $account->getMeta('accountRole');
+ $role = $this->repository->getMetaValue($account, 'accountRole');
if (strlen($role) === 0 || $type !== AccountType::ASSET) {
$role = null;
}
- $currencyId = (int)$account->getMeta('currency_id');
+ $currencyId = (int)$this->repository->getMetaValue($account, 'currency_id');
$currencyCode = null;
$decimalPlaces = 2;
if ($currencyId > 0) {
@@ -166,8 +171,8 @@ class AccountTransformer extends TransformerAbstract
$monthlyPaymentDate = null;
$creditCardType = null;
if ($role === 'ccAsset' && $type === AccountType::ASSET) {
- $creditCardType = $this->getMeta($account, 'ccType');
- $monthlyPaymentDate = $this->getMeta($account, 'ccMonthlyPaymentDate');
+ $creditCardType = $this->repository->getMetaValue($account, 'ccType');
+ $monthlyPaymentDate = $this->repository->getMetaValue($account, 'ccMonthlyPaymentDate');
}
$openingBalance = null;
@@ -192,12 +197,12 @@ class AccountTransformer extends TransformerAbstract
'currency_code' => $currencyCode,
'current_balance' => round(app('steam')->balance($account, $date), $decimalPlaces),
'current_balance_date' => $date->format('Y-m-d'),
- 'notes' => null,
+ 'notes' => $this->repository->getNote($account),
'monthly_payment_date' => $monthlyPaymentDate,
'credit_card_type' => $creditCardType,
- 'account_number' => $this->getMeta($account, 'accountNumber'),
+ 'account_number' => $this->repository->getMetaValue($account, 'accountNumber'),
'iban' => $account->iban,
- 'bic' => $this->getMeta($account, 'BIC'),
+ 'bic' => $this->repository->getMetaValue($account, 'BIC'),
'virtual_balance' => round($account->virtual_balance, $decimalPlaces),
'opening_balance' => $openingBalance,
'opening_balance_date' => $openingBalanceDate,
@@ -210,33 +215,6 @@ class AccountTransformer extends TransformerAbstract
],
];
- /** @var Note $note */
- $note = $account->notes()->first();
- if (!is_null($note)) {
- $data['notes'] = $note->text; // @codeCoverageIgnore
- }
-
return $data;
}
-
- /**
- * Get meta data field for account.
- *
- * @codeCoverageIgnore
- *
- * @param Account $account
- * @param string $field
- *
- * @return null|string
- */
- private function getMeta(Account $account, string $field): ?string
- {
- $result = $account->getMeta($field);
- if (strlen($result) === 0) {
- return null;
- }
-
- return $result;
- }
-
}