refactor ClientWidgetContactController and ClientWidgetPageReferenceListController; replace caching logic with action classes for widget data retrieval, enhancing code clarity and maintainability
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\FindContactWidgetBySlugTask;
|
||||
use App\Containers\Widget\UI\API\Transformers\ContactWidgetResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetContactWidgetPageAction
|
||||
{
|
||||
public function __construct(private FindContactWidgetBySlugTask $findContactWidgetBySlugTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $slug): ContactWidgetResource
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::CONTACT_WIDGET_PREFIX->value . $slug,
|
||||
now()->addHours(12),
|
||||
function () use ($slug) {
|
||||
return new ContactWidgetResource(
|
||||
$this->findContactWidgetBySlugTask->run($slug)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\FindPageReferenceListBySlugTask;
|
||||
use App\Containers\Widget\UI\API\Transformers\PageReferenceListResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetPageReferenceListPageAction
|
||||
{
|
||||
public function __construct(private FindPageReferenceListBySlugTask $findPageReferenceListBySlugTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $slug): PageReferenceListResource
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::PAGE_REFERENCE_LIST_PREFIX->value . $slug,
|
||||
now()->addWeek(),
|
||||
function () use ($slug) {
|
||||
return new PageReferenceListResource(
|
||||
$this->findPageReferenceListBySlugTask->run($slug)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Widget\Models\ContactWidget;
|
||||
use App\Ship\Exceptions\NotFoundException;
|
||||
|
||||
class FindContactWidgetBySlugTask
|
||||
{
|
||||
public function run(string $slug): ContactWidget
|
||||
{
|
||||
$contactWidget = ContactWidget::query()
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
return $contactWidget;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Widget\Models\PageReferenceList;
|
||||
use App\Ship\Exceptions\NotFoundException;
|
||||
|
||||
class FindPageReferenceListBySlugTask
|
||||
{
|
||||
public function run(string $slug): PageReferenceList
|
||||
{
|
||||
$pageReferenceList = PageReferenceList::query()
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
|
||||
return $pageReferenceList;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-15
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user