From ddb222cc72adaf984a28c84591392fb0bcba6243 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 10 Jul 2021 07:29:39 +0200 Subject: [PATCH] Various code cleanup. --- .../Insight/Income/AccountController.php | 6 -- .../Insight/Income/CategoryController.php | 1 - .../Insight/Transfer/AccountController.php | 2 - .../Insight/Transfer/TagController.php | 1 - .../TransactionLink/UpdateController.php | 3 - .../User/PreferencesController.php | 8 +-- .../Webhook/Sha3SignatureGenerator.php | 10 ++- .../Account/AccountRepository.php | 18 ++--- .../Journal/JournalRepositoryInterface.php | 2 - .../LinkType/LinkTypeRepository.php | 6 -- .../Internal/Update/GroupCloneService.php | 1 - .../Internal/Update/GroupUpdateService.php | 1 - .../Internal/Update/JournalUpdateService.php | 3 - app/Support/Preferences.php | 13 ++++ app/Validation/FireflyValidator.php | 66 ++++++------------- 15 files changed, 52 insertions(+), 89 deletions(-) diff --git a/app/Api/V1/Controllers/Insight/Income/AccountController.php b/app/Api/V1/Controllers/Insight/Income/AccountController.php index 3a3a72ee87..30e6b3a9e5 100644 --- a/app/Api/V1/Controllers/Insight/Income/AccountController.php +++ b/app/Api/V1/Controllers/Insight/Income/AccountController.php @@ -38,7 +38,6 @@ use Illuminate\Http\JsonResponse; * * Shows income information grouped or limited by date. * Ie. all income grouped by account + currency. - * See reference nr. 75 */ class AccountController extends Controller { @@ -74,9 +73,6 @@ class AccountController extends Controller } /** - * See reference nr. 76 - * See reference nr. 77 - * * @param GenericRequest $request * * @return JsonResponse @@ -104,8 +100,6 @@ class AccountController extends Controller } /** - * See reference nr. 78 - * * @param GenericRequest $request * * @return JsonResponse diff --git a/app/Api/V1/Controllers/Insight/Income/CategoryController.php b/app/Api/V1/Controllers/Insight/Income/CategoryController.php index 69388c113e..ffc84536fb 100644 --- a/app/Api/V1/Controllers/Insight/Income/CategoryController.php +++ b/app/Api/V1/Controllers/Insight/Income/CategoryController.php @@ -35,7 +35,6 @@ use Illuminate\Support\Collection; /** * Class CategoryController -* See reference nr. 79 */ class CategoryController extends Controller { diff --git a/app/Api/V1/Controllers/Insight/Transfer/AccountController.php b/app/Api/V1/Controllers/Insight/Transfer/AccountController.php index e3239b578a..f20716ab84 100644 --- a/app/Api/V1/Controllers/Insight/Transfer/AccountController.php +++ b/app/Api/V1/Controllers/Insight/Transfer/AccountController.php @@ -58,8 +58,6 @@ class AccountController extends Controller } /** -* See reference nr. 80 -* See reference nr. 81 * * @param GenericRequest $request * diff --git a/app/Api/V1/Controllers/Insight/Transfer/TagController.php b/app/Api/V1/Controllers/Insight/Transfer/TagController.php index b0366d70d7..e67b01ac9c 100644 --- a/app/Api/V1/Controllers/Insight/Transfer/TagController.php +++ b/app/Api/V1/Controllers/Insight/Transfer/TagController.php @@ -39,7 +39,6 @@ class TagController extends Controller /** * TagController constructor. -* See reference nr. 82 */ public function __construct() { diff --git a/app/Api/V1/Controllers/Models/TransactionLink/UpdateController.php b/app/Api/V1/Controllers/Models/TransactionLink/UpdateController.php index 140147105a..3e9459070b 100644 --- a/app/Api/V1/Controllers/Models/TransactionLink/UpdateController.php +++ b/app/Api/V1/Controllers/Models/TransactionLink/UpdateController.php @@ -26,7 +26,6 @@ namespace FireflyIII\Api\V1\Controllers\Models\TransactionLink; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Models\TransactionLink\UpdateRequest; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionJournalLink; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; @@ -71,8 +70,6 @@ class UpdateController extends Controller * @param TransactionJournalLink $journalLink * * @return JsonResponse - * -* See reference nr. 84 */ public function update(UpdateRequest $request, TransactionJournalLink $journalLink): JsonResponse { diff --git a/app/Api/V1/Controllers/User/PreferencesController.php b/app/Api/V1/Controllers/User/PreferencesController.php index a0223f95df..5c7b1df7c7 100644 --- a/app/Api/V1/Controllers/User/PreferencesController.php +++ b/app/Api/V1/Controllers/User/PreferencesController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\User; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\User\PreferenceStoreRequest; use FireflyIII\Api\V1\Requests\User\PreferenceUpdateRequest; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Preference; use FireflyIII\Transformers\PreferenceTransformer; use Illuminate\Http\JsonResponse; @@ -47,13 +48,12 @@ class PreferencesController extends Controller * List all of them. * * @return JsonResponse - * @throws \FireflyIII\Exceptions\FireflyException + * @throws FireflyException * @codeCoverageIgnore */ public function index(): JsonResponse { -// See reference nr. 83 - $collection = auth()->user()->preferences()->get(); + $collection = app('preferences')->all(); $manager = $this->getManager(); $count = $collection->count(); $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; @@ -119,7 +119,7 @@ class PreferencesController extends Controller * @param Preference $preference * * @return JsonResponse - * @throws \FireflyIII\Exceptions\FireflyException + * @throws FireflyException */ public function update(PreferenceUpdateRequest $request, Preference $preference): JsonResponse { diff --git a/app/Helpers/Webhook/Sha3SignatureGenerator.php b/app/Helpers/Webhook/Sha3SignatureGenerator.php index aa5b7e1ce6..7ef80dedbd 100644 --- a/app/Helpers/Webhook/Sha3SignatureGenerator.php +++ b/app/Helpers/Webhook/Sha3SignatureGenerator.php @@ -23,8 +23,11 @@ declare(strict_types=1); namespace FireflyIII\Helpers\Webhook; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\WebhookMessage; use JsonException; +use Log; + /** * Class Sha3SignatureGenerator @@ -41,8 +44,11 @@ class Sha3SignatureGenerator implements SignatureGeneratorInterface try { $json = json_encode($message->message, JSON_THROW_ON_ERROR); } catch (JsonException $e) { -// See reference nr. 87 - return sprintf('t=1,v%d=err-invalid-signature', $this->getVersion()); + Log::error('Could not generate hash.'); + Log::error(sprintf('JSON value: %s', $message->message)); + Log::error($e->getMessage()); + Log::error($e->getTraceAsString()); + throw new FireflyException('Could not generate JSON for SHA3 hash.', $e); } // signature v1 is generated using the following structure: diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ff753aeba2..f36ce1636e 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -149,21 +149,17 @@ class AccountRepository implements AccountRepositoryInterface } Log::debug(sprintf('Searching for account named "%s" (of user #%d) of the following type(s)', $name, $this->user->id), ['types' => $types]); - $accounts = $query->get(['accounts.*']); - - // See reference nr. 10 - + $query->where('accounts.name', $name); /** @var Account $account */ - foreach ($accounts as $account) { - if ($account->name === $name) { - Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id)); + $account = $query->first(['accounts.*']); + if (null === $account) { + Log::debug(sprintf('There is no account with name "%s" of types', $name), $types); - return $account; - } + return null; } - Log::debug(sprintf('There is no account with name "%s" of types', $name), $types); + Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id)); - return null; + return $account; } /** diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index ea595eeff0..0bf2fc4373 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -99,8 +99,6 @@ interface JournalRepositoryInterface public function getLast(): ?TransactionJournal; /** - * See reference nr. 4 - * * @param TransactionJournalLink $link * * @return string diff --git a/app/Repositories/LinkType/LinkTypeRepository.php b/app/Repositories/LinkType/LinkTypeRepository.php index f7e260e4be..c2196efa41 100644 --- a/app/Repositories/LinkType/LinkTypeRepository.php +++ b/app/Repositories/LinkType/LinkTypeRepository.php @@ -24,8 +24,6 @@ namespace FireflyIII\Repositories\LinkType; use Exception; use FireflyIII\Events\DestroyedTransactionLink; -use FireflyIII\Events\StoredTransactionLink; -use FireflyIII\Events\UpdatedTransactionLink; use FireflyIII\Models\LinkType; use FireflyIII\Models\Note; use FireflyIII\Models\TransactionJournal; @@ -281,8 +279,6 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface // make note in noteable: $this->setNoteText($link, (string)$information['notes']); - event(new StoredTransactionLink($link)); - return $link; } @@ -352,8 +348,6 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface $this->setNoteText($journalLink, $data['notes']); } - event(new UpdatedTransactionLink($journalLink)); - return $journalLink; } diff --git a/app/Services/Internal/Update/GroupCloneService.php b/app/Services/Internal/Update/GroupCloneService.php index ffaf0113f1..8bf0eb443f 100644 --- a/app/Services/Internal/Update/GroupCloneService.php +++ b/app/Services/Internal/Update/GroupCloneService.php @@ -35,7 +35,6 @@ use FireflyIII\Models\TransactionJournalMeta; /** * Class GroupCloneService -* See reference nr. 92 */ class GroupCloneService { diff --git a/app/Services/Internal/Update/GroupUpdateService.php b/app/Services/Internal/Update/GroupUpdateService.php index b5a44a7d84..9648b38c22 100644 --- a/app/Services/Internal/Update/GroupUpdateService.php +++ b/app/Services/Internal/Update/GroupUpdateService.php @@ -32,7 +32,6 @@ use Log; /** * Class GroupUpdateService - * See reference nr. 91 */ class GroupUpdateService { diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php index 77c533e098..0a18d2f21e 100644 --- a/app/Services/Internal/Update/JournalUpdateService.php +++ b/app/Services/Internal/Update/JournalUpdateService.php @@ -48,7 +48,6 @@ use Log; * Class to centralise code that updates a journal given the input by system. * * Class JournalUpdateService - * See reference nr. 93 */ class JournalUpdateService { @@ -163,8 +162,6 @@ class JournalUpdateService $this->updateAmount(); $this->updateForeignAmount(); - // See reference nr. 94 - app('preferences')->mark(); $this->transactionJournal->refresh(); diff --git a/app/Support/Preferences.php b/app/Support/Preferences.php index f7b8d59e4e..9262a0fc88 100644 --- a/app/Support/Preferences.php +++ b/app/Support/Preferences.php @@ -144,6 +144,19 @@ class Preferences return $result; } + /** + * @return Collection + */ + public function all(): Collection + { + $user = auth()->user(); + if(null === $user) { + return new Collection; + } + + return Preference::where('user_id', $user->id)->get(); + } + /** * @param User $user * @param string $name diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 3e9c85e708..971292ac58 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -28,7 +28,6 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountType; -use FireflyIII\Models\Budget; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\TransactionType; use FireflyIII\Models\Webhook; @@ -266,16 +265,8 @@ class FireflyValidator extends Validator if ('set_budget' === $actionType) { /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepositoryInterface::class); - $budgets = $repository->getBudgets(); - // count budgets, should have at least one - // See reference nr. 102 - $count = $budgets->filter( - function (Budget $budget) use ($value) { - return $budget->name === $value; - } - )->count(); - return 1 === $count; + return null !== $repository->findByName($value); } // if it's link to bill, verify the name of the bill. @@ -450,16 +441,14 @@ class FireflyValidator extends Validator $type = AccountType::find($this->data['account_type_id'])->first(); $value = $this->data['name']; - $set = $user->accounts()->where('account_type_id', $type->id)->get(); - // See reference nr. 103 - /** @var Account $entry */ - foreach ($set as $entry) { - if ($entry->name === $value) { - return false; + $set = $user->accounts()->where('account_type_id', $type->id)->get(); + $result = $set->first( + function (Account $account) use ($value) { + return $account->name === $value; } - } + ); - return true; + return null === $result; } /** @@ -483,15 +472,13 @@ class FireflyValidator extends Validator $accountTypeIds = $accountTypes->pluck('id')->toArray(); /** @var Collection $set */ $set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get(); - // See reference nr. 104 - /** @var Account $entry */ - foreach ($set as $entry) { - if ($entry->name === $value) { - return false; + $result = $set->first( + function (Account $account) use ($value) { + return $account->name === $value; } - } + ); + return null === $result; - return true; } /** @@ -507,16 +494,13 @@ class FireflyValidator extends Validator /** @var Collection $set */ $set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); - // See reference nr. 105 - /** @var Account $entry */ - foreach ($set as $entry) { - // See reference nr. 106 - if ($entry->name === $value) { - return false; - } - } - return true; + $result = $set->first( + function (Account $account) use ($value) { + return $account->name === $value; + } + ); + return null === $result; } /** @@ -733,18 +717,8 @@ class FireflyValidator extends Validator if (null !== $exclude) { $query->where('piggy_banks.id', '!=', (int)$exclude); } - $set = $query->get(['piggy_banks.*']); - - /** @var PiggyBank $entry */ - foreach ($set as $entry) { - - $fieldValue = $entry->name; - if ($fieldValue === $value) { - return false; - } - } - - return true; + $query->where('piggy_banks.name',$value); + return null === $query->first(['piggy_banks.*']); } /**