Avoid using model methods and use repository instead

This commit is contained in:
James Cole 2018-03-19 19:39:02 +01:00
parent aecffe10d9
commit 1645490f5c
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 140 additions and 43 deletions

View File

@ -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)

View File

@ -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;
}
}

View File

@ -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.
*

View File

@ -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

View File

@ -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

View File

@ -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
*

View File

@ -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;

View File

@ -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;
}
}