From 9d826519e3d0cac286de8fae637b5317d8eeec9b Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 24 Oct 2020 18:40:17 +0200 Subject: [PATCH] Simplify code. --- .../Internal/Update/JournalUpdateService.php | 55 ++++++++++++------- .../Authentication/RemoteUserGuard.php | 12 ++-- app/Support/Search/AccountSearch.php | 30 +++++----- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php index 7593a145af..b357652e97 100644 --- a/app/Services/Internal/Update/JournalUpdateService.php +++ b/app/Services/Internal/Update/JournalUpdateService.php @@ -54,17 +54,32 @@ class JournalUpdateService { use JournalServiceTrait; - private BillRepositoryInterface $billRepository; - private CurrencyRepositoryInterface $currencyRepository; - private array $data; - private Account $destinationAccount; - private Transaction $destinationTransaction; - private array $metaDate; - private array $metaString; - private Account $sourceAccount; - private Transaction $sourceTransaction; - private TransactionGroup $transactionGroup; - private TransactionJournal $transactionJournal; + /** @var BillRepositoryInterface */ + private $billRepository; + /** @var CurrencyRepositoryInterface */ + private $currencyRepository; + /** @var array The data to update the journal with. */ + private $data; + /** @var Account The destination account. */ + private $destinationAccount; + /** @var Transaction */ + private $destinationTransaction; + /** @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. @@ -363,9 +378,9 @@ class JournalUpdateService $sourceName = $this->data['source_name'] ?? null; if (!$this->hasFields(['source_id', 'source_name'])) { - $sourceAccount = $this->getOriginalSourceAccount(); - $sourceId = $sourceAccount->id; - $sourceName = $sourceAccount->name; + $origSourceAccount = $this->getOriginalSourceAccount(); + $sourceId = $origSourceAccount->id; + $sourceName = $origSourceAccount->name; } // make new account validator. @@ -402,9 +417,9 @@ class JournalUpdateService return; } - $sourceTransaction = $this->getSourceTransaction(); - $sourceTransaction->account()->associate($source); - $sourceTransaction->save(); + $origSourceTransaction = $this->getSourceTransaction(); + $origSourceTransaction->account()->associate($source); + $origSourceTransaction->save(); $destTransaction = $this->getDestinationTransaction(); $destTransaction->account()->associate($destination); @@ -436,9 +451,9 @@ class JournalUpdateService return; } - $sourceTransaction = $this->getSourceTransaction(); - $sourceTransaction->amount = app('steam')->negative($amount); - $sourceTransaction->save(); + $origSourceTransaction = $this->getSourceTransaction(); + $origSourceTransaction->amount = app('steam')->negative($amount); + $origSourceTransaction->save(); $destTransaction = $this->getDestinationTransaction(); diff --git a/app/Support/Authentication/RemoteUserGuard.php b/app/Support/Authentication/RemoteUserGuard.php index c2b8f01ea8..7a333e7006 100644 --- a/app/Support/Authentication/RemoteUserGuard.php +++ b/app/Support/Authentication/RemoteUserGuard.php @@ -75,20 +75,20 @@ class RemoteUserGuard implements Guard throw new FireflyException('The guard header was unexpectedly empty. See the logs.'); } - /** @var User $user */ - $user = $this->provider->retrieveById($userID); + /** @var User $retrievedUser */ + $retrievedUser = $this->provider->retrieveById($userID); // store email address if present in header and not already set. $header = config('auth.guard_email'); $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) { - 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)); - $this->user = $user; + Log::debug(sprintf('Result of getting user from provider: %s', $retrievedUser->email)); + $this->user = $retrievedUser; } /** diff --git a/app/Support/Search/AccountSearch.php b/app/Support/Search/AccountSearch.php index a8a81d7cc3..1b781771d4 100644 --- a/app/Support/Search/AccountSearch.php +++ b/app/Support/Search/AccountSearch.php @@ -45,15 +45,10 @@ class AccountSearch implements GenericSearchInterface /** @var string */ public const SEARCH_ID = 'id'; - /** @var string */ - private $field; - /** @var string */ - private $query; - /** @var array */ - private $types; - - /** @var User */ - private $user; + private string $field; + private string $query; + private array $types; + private User $user; public function __construct() { @@ -66,7 +61,7 @@ class AccountSearch implements GenericSearchInterface public function search(): Collection { - $query = $this->user->accounts() + $searchQuery = $this->user->accounts() ->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id') ->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id') ->whereIn('account_types.type', $this->types); @@ -75,7 +70,7 @@ class AccountSearch implements GenericSearchInterface switch ($this->field) { default: case self::SEARCH_ALL: - $query->where( + $searchQuery->where( static function (Builder $q) use ($like) { $q->where('accounts.id', 'LIKE', $like); $q->orWhere('accounts.name', 'LIKE', $like); @@ -83,7 +78,7 @@ class AccountSearch implements GenericSearchInterface } ); // meta data: - $query->orWhere( + $searchQuery->orWhere( static function (Builder $q) use ($originalQuery) { $json = json_encode($originalQuery, JSON_THROW_ON_ERROR); $q->where('account_meta.name', '=', 'account_number'); @@ -92,17 +87,17 @@ class AccountSearch implements GenericSearchInterface ); break; case self::SEARCH_ID: - $query->where('accounts.id', '=', (int)$originalQuery); + $searchQuery->where('accounts.id', '=', (int)$originalQuery); break; case self::SEARCH_NAME: - $query->where('accounts.name', 'LIKE', $like); + $searchQuery->where('accounts.name', 'LIKE', $like); break; case self::SEARCH_IBAN: - $query->where('accounts.iban', 'LIKE', $like); + $searchQuery->where('accounts.iban', 'LIKE', $like); break; case self::SEARCH_NUMBER: // meta data: - $query->Where( + $searchQuery->Where( static function (Builder $q) use ($originalQuery) { $json = json_encode($originalQuery, JSON_THROW_ON_ERROR); $q->where('account_meta.name', 'account_number'); @@ -111,7 +106,8 @@ class AccountSearch implements GenericSearchInterface ); break; } - return $query->distinct()->get(['accounts.*']); + + return $searchQuery->distinct()->get(['accounts.*']); } /**