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
8 changed files with 140 additions and 43 deletions

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