From 10d25cc0224eb23e15f1e9f0b356036696a4ca00 Mon Sep 17 00:00:00 2001 From: pushrbx Date: Wed, 28 Jun 2023 16:43:11 +0100 Subject: [PATCH] refactorings --- app/Anime.php | 2 +- app/Concerns/ResolvesPaginatorParams.php | 2 +- app/Dto/Concerns/PreparesData.php | 2 +- app/Rules/MaxResultsPerPageRule.php | 10 +++------- app/Support/helpers.php | 7 +++++++ 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/Anime.php b/app/Anime.php index b3a9f03..1d72a26 100644 --- a/app/Anime.php +++ b/app/Anime.php @@ -381,7 +381,7 @@ class Anime extends JikanApiSearchableModel { return [ [ - "field" => "_text_match(buckets:" . App::make("jikan-config")->maxResultsPerPage() . ")", + "field" => "_text_match(buckets:" . max_results_per_page() . ")", "direction" => "desc" ], [ diff --git a/app/Concerns/ResolvesPaginatorParams.php b/app/Concerns/ResolvesPaginatorParams.php index 03dc91a..f958cea 100644 --- a/app/Concerns/ResolvesPaginatorParams.php +++ b/app/Concerns/ResolvesPaginatorParams.php @@ -8,7 +8,7 @@ trait ResolvesPaginatorParams { private function getPaginatorParams(?int $limit = null, ?int $page = null): array { - $default_max_results_per_page = App::make("jikan-config")->maxResultsPerPage(); + $default_max_results_per_page = max_results_per_page(); $limit = $limit ?? $default_max_results_per_page; $page = $page ?? 1; diff --git a/app/Dto/Concerns/PreparesData.php b/app/Dto/Concerns/PreparesData.php index 6da4080..a33e26f 100644 --- a/app/Dto/Concerns/PreparesData.php +++ b/app/Dto/Concerns/PreparesData.php @@ -20,7 +20,7 @@ trait PreparesData // let's always set the limit parameter to the globally configured default value if (property_exists(static::class, "limit") && !$properties->has("limit")) { /** @noinspection PhpUndefinedFieldInspection */ - $properties->put("limit", App::make("jikan-config")->maxResultsPerPage( + $properties->put("limit", max_results_per_page( property_exists(static::class, "defaultLimit") ? static::$defaultLimit : null)); } diff --git a/app/Rules/MaxResultsPerPageRule.php b/app/Rules/MaxResultsPerPageRule.php index 62df997..4d5bebb 100644 --- a/app/Rules/MaxResultsPerPageRule.php +++ b/app/Rules/MaxResultsPerPageRule.php @@ -28,7 +28,7 @@ final class MaxResultsPerPageRule implements Rule $value = intval($value); } - if ($value > $this->maxResultsPerPage()) { + if ($value > max_results_per_page()) { return false; } @@ -37,11 +37,7 @@ final class MaxResultsPerPageRule implements Rule public function message(): array|string { - return "Value {$this->value} is higher than the configured '{$this->maxResultsPerPage()}' max value."; - } - - private function maxResultsPerPage(): int - { - return (int) App::make("jikan-config")->maxResultsPerPage($this->fallbackLimit); + $mrpp = max_results_per_page(); + return "Value {$this->value} is higher than the configured '$mrpp' max value."; } } diff --git a/app/Support/helpers.php b/app/Support/helpers.php index 0ed100a..ddeb3c7 100644 --- a/app/Support/helpers.php +++ b/app/Support/helpers.php @@ -54,3 +54,10 @@ if (!function_exists('to_boolean')) { } } + +if (!function_exists('max_results_per_page')) { + function max_results_per_page(?int $fallbackLimit = null): int + { + return app()->make("jikan-config")->maxResultsPerPage($fallbackLimit); + } +}