Simplify code.

This commit is contained in:
James Cole
2020-10-24 18:40:17 +02:00
parent 3979e12043
commit 9d826519e3
3 changed files with 54 additions and 43 deletions

View File

@@ -54,17 +54,32 @@ class JournalUpdateService
{ {
use JournalServiceTrait; use JournalServiceTrait;
private BillRepositoryInterface $billRepository; /** @var BillRepositoryInterface */
private CurrencyRepositoryInterface $currencyRepository; private $billRepository;
private array $data; /** @var CurrencyRepositoryInterface */
private Account $destinationAccount; private $currencyRepository;
private Transaction $destinationTransaction; /** @var array The data to update the journal with. */
private array $metaDate; private $data;
private array $metaString; /** @var Account The destination account. */
private Account $sourceAccount; private $destinationAccount;
private Transaction $sourceTransaction; /** @var Transaction */
private TransactionGroup $transactionGroup; private $destinationTransaction;
private TransactionJournal $transactionJournal; /** @var array All meta values that are dates. */
private $metaDate;
/** @var array All meta values that are strings. */
private $metaString;
/** @var Account Source account of the journal */
private $sourceAccount;
/** @var Transaction Source transaction of the journal. */
private $sourceTransaction;
/** @var TransactionGroup The parent group. */
private $transactionGroup;
/** @var TransactionJournal The journal to update. */
private $transactionJournal;
/** @var Account If new account info is submitted, this array will hold the valid destination. */
private $validDestination;
/** @var Account If new account info is submitted, this array will hold the valid source. */
private $validSource;
/** /**
* JournalUpdateService constructor. * JournalUpdateService constructor.
@@ -363,9 +378,9 @@ class JournalUpdateService
$sourceName = $this->data['source_name'] ?? null; $sourceName = $this->data['source_name'] ?? null;
if (!$this->hasFields(['source_id', 'source_name'])) { if (!$this->hasFields(['source_id', 'source_name'])) {
$sourceAccount = $this->getOriginalSourceAccount(); $origSourceAccount = $this->getOriginalSourceAccount();
$sourceId = $sourceAccount->id; $sourceId = $origSourceAccount->id;
$sourceName = $sourceAccount->name; $sourceName = $origSourceAccount->name;
} }
// make new account validator. // make new account validator.
@@ -402,9 +417,9 @@ class JournalUpdateService
return; return;
} }
$sourceTransaction = $this->getSourceTransaction(); $origSourceTransaction = $this->getSourceTransaction();
$sourceTransaction->account()->associate($source); $origSourceTransaction->account()->associate($source);
$sourceTransaction->save(); $origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction(); $destTransaction = $this->getDestinationTransaction();
$destTransaction->account()->associate($destination); $destTransaction->account()->associate($destination);
@@ -436,9 +451,9 @@ class JournalUpdateService
return; return;
} }
$sourceTransaction = $this->getSourceTransaction(); $origSourceTransaction = $this->getSourceTransaction();
$sourceTransaction->amount = app('steam')->negative($amount); $origSourceTransaction->amount = app('steam')->negative($amount);
$sourceTransaction->save(); $origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction(); $destTransaction = $this->getDestinationTransaction();

View File

@@ -75,20 +75,20 @@ class RemoteUserGuard implements Guard
throw new FireflyException('The guard header was unexpectedly empty. See the logs.'); throw new FireflyException('The guard header was unexpectedly empty. See the logs.');
} }
/** @var User $user */ /** @var User $retrievedUser */
$user = $this->provider->retrieveById($userID); $retrievedUser = $this->provider->retrieveById($userID);
// store email address if present in header and not already set. // store email address if present in header and not already set.
$header = config('auth.guard_email'); $header = config('auth.guard_email');
$emailAddress = request()->server($header) ?? null; $emailAddress = request()->server($header) ?? null;
$preference = app('preferences')->getForUser($user, 'remote_guard_alt_email', null); $preference = app('preferences')->getForUser($retrievedUser, 'remote_guard_alt_email', null);
if (null !== $emailAddress && null === $preference && $emailAddress !== $userID) { if (null !== $emailAddress && null === $preference && $emailAddress !== $userID) {
app('preferences')->setForUser($user, 'remote_guard_alt_email', $emailAddress); app('preferences')->setForUser($retrievedUser, 'remote_guard_alt_email', $emailAddress);
} }
Log::debug(sprintf('Result of getting user from provider: %s', $user->email)); Log::debug(sprintf('Result of getting user from provider: %s', $retrievedUser->email));
$this->user = $user; $this->user = $retrievedUser;
} }
/** /**

View File

@@ -45,15 +45,10 @@ class AccountSearch implements GenericSearchInterface
/** @var string */ /** @var string */
public const SEARCH_ID = 'id'; public const SEARCH_ID = 'id';
/** @var string */ private string $field;
private $field; private string $query;
/** @var string */ private array $types;
private $query; private User $user;
/** @var array */
private $types;
/** @var User */
private $user;
public function __construct() public function __construct()
{ {
@@ -66,7 +61,7 @@ class AccountSearch implements GenericSearchInterface
public function search(): Collection public function search(): Collection
{ {
$query = $this->user->accounts() $searchQuery = $this->user->accounts()
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id') ->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id') ->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->whereIn('account_types.type', $this->types); ->whereIn('account_types.type', $this->types);
@@ -75,7 +70,7 @@ class AccountSearch implements GenericSearchInterface
switch ($this->field) { switch ($this->field) {
default: default:
case self::SEARCH_ALL: case self::SEARCH_ALL:
$query->where( $searchQuery->where(
static function (Builder $q) use ($like) { static function (Builder $q) use ($like) {
$q->where('accounts.id', 'LIKE', $like); $q->where('accounts.id', 'LIKE', $like);
$q->orWhere('accounts.name', 'LIKE', $like); $q->orWhere('accounts.name', 'LIKE', $like);
@@ -83,7 +78,7 @@ class AccountSearch implements GenericSearchInterface
} }
); );
// meta data: // meta data:
$query->orWhere( $searchQuery->orWhere(
static function (Builder $q) use ($originalQuery) { static function (Builder $q) use ($originalQuery) {
$json = json_encode($originalQuery, JSON_THROW_ON_ERROR); $json = json_encode($originalQuery, JSON_THROW_ON_ERROR);
$q->where('account_meta.name', '=', 'account_number'); $q->where('account_meta.name', '=', 'account_number');
@@ -92,17 +87,17 @@ class AccountSearch implements GenericSearchInterface
); );
break; break;
case self::SEARCH_ID: case self::SEARCH_ID:
$query->where('accounts.id', '=', (int)$originalQuery); $searchQuery->where('accounts.id', '=', (int)$originalQuery);
break; break;
case self::SEARCH_NAME: case self::SEARCH_NAME:
$query->where('accounts.name', 'LIKE', $like); $searchQuery->where('accounts.name', 'LIKE', $like);
break; break;
case self::SEARCH_IBAN: case self::SEARCH_IBAN:
$query->where('accounts.iban', 'LIKE', $like); $searchQuery->where('accounts.iban', 'LIKE', $like);
break; break;
case self::SEARCH_NUMBER: case self::SEARCH_NUMBER:
// meta data: // meta data:
$query->Where( $searchQuery->Where(
static function (Builder $q) use ($originalQuery) { static function (Builder $q) use ($originalQuery) {
$json = json_encode($originalQuery, JSON_THROW_ON_ERROR); $json = json_encode($originalQuery, JSON_THROW_ON_ERROR);
$q->where('account_meta.name', 'account_number'); $q->where('account_meta.name', 'account_number');
@@ -111,7 +106,8 @@ class AccountSearch implements GenericSearchInterface
); );
break; break;
} }
return $query->distinct()->get(['accounts.*']);
return $searchQuery->distinct()->get(['accounts.*']);
} }
/** /**