Update find methods to return null

This commit is contained in:
James Cole 2018-02-16 15:19:19 +01:00
parent 28b00f6507
commit 33db99ffd3
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
16 changed files with 142 additions and 34 deletions

View File

@ -197,7 +197,7 @@ class ExpenseReportController extends Controller
$collection->push($expenseAccount); $collection->push($expenseAccount);
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]); $revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (!is_null($revenue->id)) { if (!is_null($revenue)) {
$collection->push($revenue); $collection->push($revenue);
} }
$combined[$expenseAccount->name] = $collection; $combined[$expenseAccount->name] = $collection;

View File

@ -313,7 +313,7 @@ class ExpenseController extends Controller
$collection->push($expenseAccount); $collection->push($expenseAccount);
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]); $revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (!is_null($revenue->id)) { if (!is_null($revenue)) {
$collection->push($revenue); $collection->push($revenue);
} }
$combined[$expenseAccount->name] = $collection; $combined[$expenseAccount->name] = $collection;

View File

@ -114,7 +114,7 @@ class ImportBill
Log::debug(sprintf('Finding bill with ID #%d', $this->id['value'])); Log::debug(sprintf('Finding bill with ID #%d', $this->id['value']));
/** @var Bill $bill */ /** @var Bill $bill */
$bill = $this->repository->find(intval($this->id['value'])); $bill = $this->repository->find(intval($this->id['value']));
if (null !== $bill->id) { if (null !== $bill) {
Log::debug(sprintf('Found unmapped bill by ID (#%d): %s', $bill->id, $bill->name)); Log::debug(sprintf('Found unmapped bill by ID (#%d): %s', $bill->id, $bill->name));
return $bill; return $bill;
@ -199,7 +199,7 @@ class ImportBill
$search = intval($array['mapped']); $search = intval($array['mapped']);
$bill = $this->repository->find($search); $bill = $this->repository->find($search);
if (null === $bill->id) { if (null === $bill) {
Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search)); Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search));
return new Bill; return new Bill;

View File

@ -44,12 +44,13 @@ use Validator;
*/ */
class AccountRepository implements AccountRepositoryInterface class AccountRepository implements AccountRepositoryInterface
{ {
/** @var User */ /** @var User */
private $user; private $user;
use FindAccountsTrait;
/** @var array */ /** @var array */
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC']; private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
use FindAccountsTrait;
/** @var array */ /** @var array */
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC']; private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */ /** @var array */
@ -91,6 +92,16 @@ class AccountRepository implements AccountRepositoryInterface
return true; return true;
} }
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account
{
return $this->user->accounts()->find($accountId);
}
/** /**
* Return account type by string. * Return account type by string.
* *
@ -336,7 +347,7 @@ class AccountRepository implements AccountRepositoryInterface
// account may exist already: // account may exist already:
$existingAccount = $this->findByName($data['name'], [$type]); $existingAccount = $this->findByName($data['name'], [$type]);
if (null !== $existingAccount->id) { if (null !== $existingAccount) {
Log::warning(sprintf('There already is an account named "%s" of type "%s".', $data['name'], $type)); Log::warning(sprintf('There already is an account named "%s" of type "%s".', $data['name'], $type));
return $existingAccount; return $existingAccount;

View File

@ -57,10 +57,18 @@ interface AccountRepositoryInterface
/** /**
* @param int $accountId * @param int $accountId
* *
* @deprecated
* @return Account * @return Account
*/ */
public function find(int $accountId): Account; public function find(int $accountId): Account;
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account;
/** /**
* @param string $number * @param string $number
* @param array $types * @param array $types
@ -81,9 +89,9 @@ interface AccountRepositoryInterface
* @param string $name * @param string $name
* @param array $types * @param array $types
* *
* @return Account * @return Account|null
*/ */
public function findByName(string $name, array $types): Account; public function findByName(string $name, array $types): ?Account;
/** /**
* Return account type by string. * Return account type by string.

View File

@ -109,9 +109,9 @@ trait FindAccountsTrait
* @param string $name * @param string $name
* @param array $types * @param array $types
* *
* @return Account * @return Account|null
*/ */
public function findByName(string $name, array $types): Account public function findByName(string $name, array $types): ?Account
{ {
$query = $this->user->accounts(); $query = $this->user->accounts();
@ -132,7 +132,7 @@ trait FindAccountsTrait
} }
Log::debug(sprintf('There is no account with name "%s" or types', $name), $types); Log::debug(sprintf('There is no account with name "%s" or types', $name), $types);
return new Account; return null;
} }
/** /**

View File

@ -66,14 +66,12 @@ class BillRepository implements BillRepositoryInterface
* *
* @return Bill * @return Bill
*/ */
public function find(int $billId): Bill public function find(int $billId): ?Bill
{ {
$bill = $this->user->bills()->find($billId); /** @var Bill $res */
if (null === $bill) { $res = $this->user->bills()->find($billId);
$bill = new Bill;
}
return $bill; return $res;
} }
/** /**
@ -83,7 +81,7 @@ class BillRepository implements BillRepositoryInterface
* *
* @return Bill * @return Bill
*/ */
public function findByName(string $name): Bill public function findByName(string $name): ?Bill
{ {
$bills = $this->user->bills()->get(['bills.*']); $bills = $this->user->bills()->get(['bills.*']);
@ -94,7 +92,7 @@ class BillRepository implements BillRepositoryInterface
} }
} }
return new Bill; return null;
} }
/** /**

View File

@ -47,18 +47,18 @@ interface BillRepositoryInterface
* *
* @param int $billId * @param int $billId
* *
* @return Bill * @return Bill|null
*/ */
public function find(int $billId): Bill; public function find(int $billId): ?Bill;
/** /**
* Find a bill by name. * Find a bill by name.
* *
* @param string $name * @param string $name
* *
* @return Bill * @return Bill|null
*/ */
public function findByName(string $name): Bill; public function findByName(string $name): ?Bill;
/** /**
* @return Collection * @return Collection

View File

@ -194,9 +194,9 @@ class BudgetRepository implements BudgetRepositoryInterface
* *
* @param string $name * @param string $name
* *
* @return Budget * @return Budget|null
*/ */
public function findByName(string $name): Budget public function findByName(string $name): ?Budget
{ {
$budgets = $this->user->budgets()->get(['budgets.*']); $budgets = $this->user->budgets()->get(['budgets.*']);
/** @var Budget $budget */ /** @var Budget $budget */
@ -206,7 +206,19 @@ class BudgetRepository implements BudgetRepositoryInterface
} }
} }
return new Budget; return null;
}
/**
* Find a budget or return NULL
*
* @param int $budgetId
*
* @return Budget|null
*/
public function findNull(int $budgetId): ?Budget
{
return $this->user->budgets()->find($budgetId);
} }
/** /**

View File

@ -73,19 +73,29 @@ interface BudgetRepositoryInterface
* Find a budget. * Find a budget.
* *
* @param int $budgetId * @param int $budgetId
* @deprecated
* *
* @return Budget * @return Budget
*/ */
public function find(int $budgetId): Budget; public function find(int $budgetId): Budget;
/**
* Find a budget or return NULL
*
* @param int $budgetId
*
* @return Budget|null
*/
public function findNull(int $budgetId): ?Budget;
/** /**
* Find a budget. * Find a budget.
* *
* @param string $name * @param string $name
* *
* @return Budget * @return Budget|null
*/ */
public function findByName(string $name): Budget; public function findByName(string $name): ?Budget;
/** /**
* This method returns the oldest journal or transaction date known to this budget. * This method returns the oldest journal or transaction date known to this budget.

View File

@ -110,6 +110,18 @@ class CategoryRepository implements CategoryRepositoryInterface
return new Category; return new Category;
} }
/**
* Find a category or return NULL
*
* @param int $categoryId
*
* @return Category|null
*/
public function findNull(int $categoryId): ?Category
{
return $this->user->categories()->find($categoryId);
}
/** /**
* @param Category $category * @param Category $category
* *

View File

@ -53,11 +53,20 @@ interface CategoryRepositoryInterface
* Find a category. * Find a category.
* *
* @param int $categoryId * @param int $categoryId
* * @deprecated
* @return Category * @return Category
*/ */
public function find(int $categoryId): Category; public function find(int $categoryId): Category;
/**
* Find a category or return NULL
*
* @param int $categoryId
*
* @return Category|null
*/
public function findNull(int $categoryId): ?Category;
/** /**
* Find a category. * Find a category.
* *

View File

@ -129,6 +129,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency; return $currency;
} }
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency
{
return TransactionCurrency::where('code', $currencyCode)->first();
}
/** /**
* Find by currency name. * Find by currency name.
* *
@ -163,6 +175,21 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return $currency; return $currency;
} }
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function findNull(int $currencyId): ?TransactionCurrency
{
/** @var TransactionCurrency $res */
$res = TransactionCurrency::find($currencyId);
return $res;
}
/** /**
* @return Collection * @return Collection
*/ */

View File

@ -60,6 +60,7 @@ interface CurrencyRepositoryInterface
* *
* @param int $currencyId * @param int $currencyId
* *
* @deprecated
* @return TransactionCurrency * @return TransactionCurrency
*/ */
public function find(int $currencyId): TransactionCurrency; public function find(int $currencyId): TransactionCurrency;
@ -67,12 +68,23 @@ interface CurrencyRepositoryInterface
/** /**
* Find by currency code. * Find by currency code.
* *
* @deprecated
*
* @param string $currencyCode * @param string $currencyCode
* *
* @return TransactionCurrency * @return TransactionCurrency
*/ */
public function findByCode(string $currencyCode): TransactionCurrency; public function findByCode(string $currencyCode): TransactionCurrency;
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency;
/** /**
* Find by currency name. * Find by currency name.
* *
@ -91,6 +103,15 @@ interface CurrencyRepositoryInterface
*/ */
public function findBySymbol(string $currencySymbol): TransactionCurrency; public function findBySymbol(string $currencySymbol): TransactionCurrency;
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function findNull(int $currencyId): ?TransactionCurrency;
/** /**
* @return Collection * @return Collection
*/ */

View File

@ -117,7 +117,7 @@ class SetDestinationAccount implements ActionInterface
{ {
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]); $account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
if (null === $account->id) { if (null === $account) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value)); Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false; return false;
@ -134,7 +134,7 @@ class SetDestinationAccount implements ActionInterface
private function findExpenseAccount() private function findExpenseAccount()
{ {
$account = $this->repository->findByName($this->action->action_value, [AccountType::EXPENSE]); $account = $this->repository->findByName($this->action->action_value, [AccountType::EXPENSE]);
if (null === $account->id) { if (null === $account) {
// create new revenue account with this name: // create new revenue account with this name:
$data = [ $data = [
'name' => $this->action->action_value, 'name' => $this->action->action_value,

View File

@ -116,7 +116,7 @@ class SetSourceAccount implements ActionInterface
{ {
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]); $account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
if (null === $account->id) { if (null === $account) {
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value)); Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false; return false;
@ -133,7 +133,7 @@ class SetSourceAccount implements ActionInterface
private function findRevenueAccount() private function findRevenueAccount()
{ {
$account = $this->repository->findByName($this->action->action_value, [AccountType::REVENUE]); $account = $this->repository->findByName($this->action->action_value, [AccountType::REVENUE]);
if (null === $account->id) { if (null === $account) {
// create new revenue account with this name: // create new revenue account with this name:
$data = [ $data = [
'name' => $this->action->action_value, 'name' => $this->action->action_value,