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);
$revenue = $this->accountRepository->findByName($expenseAccount->name, [AccountType::REVENUE]);
if (!is_null($revenue->id)) {
if (!is_null($revenue)) {
$collection->push($revenue);
}
$combined[$expenseAccount->name] = $collection;

View File

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

View File

@ -114,7 +114,7 @@ class ImportBill
Log::debug(sprintf('Finding bill with ID #%d', $this->id['value']));
/** @var Bill $bill */
$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));
return $bill;
@ -199,7 +199,7 @@ class ImportBill
$search = intval($array['mapped']);
$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));
return new Bill;

View File

@ -44,12 +44,13 @@ use Validator;
*/
class AccountRepository implements AccountRepositoryInterface
{
/** @var User */
private $user;
use FindAccountsTrait;
/** @var array */
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
use FindAccountsTrait;
/** @var array */
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
@ -91,6 +92,16 @@ class AccountRepository implements AccountRepositoryInterface
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.
*
@ -336,7 +347,7 @@ class AccountRepository implements AccountRepositoryInterface
// account may exist already:
$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));
return $existingAccount;

View File

@ -57,10 +57,18 @@ interface AccountRepositoryInterface
/**
* @param int $accountId
*
* @deprecated
* @return Account
*/
public function find(int $accountId): Account;
/**
* @param int $accountId
*
* @return Account|null
*/
public function findNull(int $accountId): ?Account;
/**
* @param string $number
* @param array $types
@ -81,9 +89,9 @@ interface AccountRepositoryInterface
* @param string $name
* @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.

View File

@ -109,9 +109,9 @@ trait FindAccountsTrait
* @param string $name
* @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();
@ -132,7 +132,7 @@ trait FindAccountsTrait
}
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
*/
public function find(int $billId): Bill
public function find(int $billId): ?Bill
{
$bill = $this->user->bills()->find($billId);
if (null === $bill) {
$bill = new Bill;
}
/** @var Bill $res */
$res = $this->user->bills()->find($billId);
return $bill;
return $res;
}
/**
@ -83,7 +81,7 @@ class BillRepository implements BillRepositoryInterface
*
* @return Bill
*/
public function findByName(string $name): Bill
public function findByName(string $name): ?Bill
{
$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
*
* @return Bill
* @return Bill|null
*/
public function find(int $billId): Bill;
public function find(int $billId): ?Bill;
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill
* @return Bill|null
*/
public function findByName(string $name): Bill;
public function findByName(string $name): ?Bill;
/**
* @return Collection

View File

@ -194,9 +194,9 @@ class BudgetRepository implements BudgetRepositoryInterface
*
* @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.*']);
/** @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.
*
* @param int $budgetId
* @deprecated
*
* @return 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.
*
* @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.

View File

@ -110,6 +110,18 @@ class CategoryRepository implements CategoryRepositoryInterface
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
*

View File

@ -53,11 +53,20 @@ interface CategoryRepositoryInterface
* Find a category.
*
* @param int $categoryId
*
* @deprecated
* @return 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.
*

View File

@ -129,6 +129,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
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.
*
@ -163,6 +175,21 @@ class CurrencyRepository implements CurrencyRepositoryInterface
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
*/

View File

@ -60,6 +60,7 @@ interface CurrencyRepositoryInterface
*
* @param int $currencyId
*
* @deprecated
* @return TransactionCurrency
*/
public function find(int $currencyId): TransactionCurrency;
@ -67,12 +68,23 @@ interface CurrencyRepositoryInterface
/**
* Find by currency code.
*
* @deprecated
*
* @param string $currencyCode
*
* @return 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.
*
@ -91,6 +103,15 @@ interface CurrencyRepositoryInterface
*/
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
*/

View File

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