From eac29d8d1cfc32c24b682b15ca723a3f9d32f66b Mon Sep 17 00:00:00 2001 From: F4ilji Date: Mon, 21 Jul 2025 22:45:55 +0500 Subject: [PATCH] refactor ClientWidgetContactController and ClientWidgetPageReferenceListController; replace caching logic with action classes for widget data retrieval, enhancing code clarity and maintainability --- .../Actions/GetContactWidgetPageAction.php | 28 +++++++++++++++++++ .../GetPageReferenceListPageAction.php | 28 +++++++++++++++++++ .../Tasks/FindContactWidgetBySlugTask.php | 18 ++++++++++++ .../Tasks/FindPageReferenceListBySlugTask.php | 18 ++++++++++++ .../ClientWidgetContactController.php | 21 ++++---------- ...lientWidgetPageReferenceListController.php | 21 ++++---------- 6 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 app/Containers/Widget/Actions/GetContactWidgetPageAction.php create mode 100644 app/Containers/Widget/Actions/GetPageReferenceListPageAction.php create mode 100644 app/Containers/Widget/Tasks/FindContactWidgetBySlugTask.php create mode 100644 app/Containers/Widget/Tasks/FindPageReferenceListBySlugTask.php diff --git a/app/Containers/Widget/Actions/GetContactWidgetPageAction.php b/app/Containers/Widget/Actions/GetContactWidgetPageAction.php new file mode 100644 index 0000000..bc8f7f3 --- /dev/null +++ b/app/Containers/Widget/Actions/GetContactWidgetPageAction.php @@ -0,0 +1,28 @@ +value . $slug, + now()->addHours(12), + function () use ($slug) { + return new ContactWidgetResource( + $this->findContactWidgetBySlugTask->run($slug) + ); + } + ); + } +} diff --git a/app/Containers/Widget/Actions/GetPageReferenceListPageAction.php b/app/Containers/Widget/Actions/GetPageReferenceListPageAction.php new file mode 100644 index 0000000..2d921ca --- /dev/null +++ b/app/Containers/Widget/Actions/GetPageReferenceListPageAction.php @@ -0,0 +1,28 @@ +value . $slug, + now()->addWeek(), + function () use ($slug) { + return new PageReferenceListResource( + $this->findPageReferenceListBySlugTask->run($slug) + ); + } + ); + } +} diff --git a/app/Containers/Widget/Tasks/FindContactWidgetBySlugTask.php b/app/Containers/Widget/Tasks/FindContactWidgetBySlugTask.php new file mode 100644 index 0000000..c5560c1 --- /dev/null +++ b/app/Containers/Widget/Tasks/FindContactWidgetBySlugTask.php @@ -0,0 +1,18 @@ +where('slug', $slug) + ->firstOrFail(); + + return $contactWidget; + } +} diff --git a/app/Containers/Widget/Tasks/FindPageReferenceListBySlugTask.php b/app/Containers/Widget/Tasks/FindPageReferenceListBySlugTask.php new file mode 100644 index 0000000..e02d6e0 --- /dev/null +++ b/app/Containers/Widget/Tasks/FindPageReferenceListBySlugTask.php @@ -0,0 +1,18 @@ +where('slug', $slug) + ->firstOrFail(); + + return $pageReferenceList; + } +} diff --git a/app/Containers/Widget/UI/API/Controllers/ClientWidgetContactController.php b/app/Containers/Widget/UI/API/Controllers/ClientWidgetContactController.php index c4992dd..5d059fd 100644 --- a/app/Containers/Widget/UI/API/Controllers/ClientWidgetContactController.php +++ b/app/Containers/Widget/UI/API/Controllers/ClientWidgetContactController.php @@ -2,26 +2,17 @@ namespace App\Containers\Widget\UI\API\Controllers; -use App\Containers\Widget\Models\ContactWidget; -use App\Containers\Widget\UI\API\Transformers\ContactWidgetResource; +use App\Containers\Widget\Actions\GetContactWidgetPageAction; use App\Ship\Controllers\Controller; -use App\Ship\Enums\CacheKeys; -use Illuminate\Support\Facades\Cache; class ClientWidgetContactController extends Controller { + public function __construct(private GetContactWidgetPageAction $getContactWidgetPageAction) + { + } + public function show(string $slug) { - return Cache::remember( - CacheKeys::CONTACT_WIDGET_PREFIX->value . $slug, - now()->addHours(12), // Кешируем на 12 часов - function () use ($slug) { - return new ContactWidgetResource( - ContactWidget::query() - ->where('slug', $slug) - ->firstOrFail() - ); - } - ); + return $this->getContactWidgetPageAction->run($slug); } } diff --git a/app/Containers/Widget/UI/API/Controllers/ClientWidgetPageReferenceListController.php b/app/Containers/Widget/UI/API/Controllers/ClientWidgetPageReferenceListController.php index 9902a99..8fd332d 100644 --- a/app/Containers/Widget/UI/API/Controllers/ClientWidgetPageReferenceListController.php +++ b/app/Containers/Widget/UI/API/Controllers/ClientWidgetPageReferenceListController.php @@ -2,26 +2,17 @@ namespace App\Containers\Widget\UI\API\Controllers; -use App\Containers\Widget\Models\PageReferenceList; -use App\Containers\Widget\UI\API\Transformers\PageReferenceListResource; +use App\Containers\Widget\Actions\GetPageReferenceListPageAction; use App\Ship\Controllers\Controller; -use App\Ship\Enums\CacheKeys; -use Illuminate\Support\Facades\Cache; class ClientWidgetPageReferenceListController extends Controller { + public function __construct(private GetPageReferenceListPageAction $getPageReferenceListPageAction) + { + } + public function show(string $slug) { - return Cache::remember( - CacheKeys::PAGE_REFERENCE_LIST_PREFIX->value . $slug, - now()->addWeek(), // Кешируем на неделю, так как справочники меняются редко - function () use ($slug) { - return new PageReferenceListResource( - PageReferenceList::query() - ->where('slug', $slug) - ->firstOrFail() - ); - } - ); + return $this->getPageReferenceListPageAction->run($slug); } }