refactor PersonController; replace caching logic with GetPersonDataAction for improved maintainability and clarity
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\User\Actions;
|
||||
|
||||
use App\Containers\User\Tasks\FindPersonBySlugTask;
|
||||
use App\Containers\User\Tasks\GetSeoForPersonTask;
|
||||
|
||||
class GetPersonDataAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FindPersonBySlugTask $findPersonBySlugTask,
|
||||
private readonly GetSeoForPersonTask $getSeoForPersonTask,
|
||||
) {}
|
||||
|
||||
public function run(string $slug): array
|
||||
{
|
||||
$personData = $this->findPersonBySlugTask->run($slug);
|
||||
$seo = $this->getSeoForPersonTask->run($personData);
|
||||
|
||||
return compact('personData', 'seo');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\User\Tasks;
|
||||
|
||||
use App\Containers\User\Models\User;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use App\Ship\Parents\Tasks\Task;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FindPersonBySlugTask
|
||||
{
|
||||
public function run(string $slug): User
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::USER_PREFIX->value . $slug,
|
||||
now()->addHours(24),
|
||||
fn() => User::with(['userDetail', 'departments_work.faculty', 'departments_teach.faculty', 'divisions', 'faculties'])
|
||||
->where('slug', $slug)
|
||||
->whereHas('userDetail')
|
||||
->firstOrFail()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\User\Tasks;
|
||||
|
||||
use App\Containers\User\Models\User;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use App\Ship\Models\Seo;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetSeoForPersonTask
|
||||
{
|
||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||
|
||||
public function run(User $personData): array|null
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::USER_PREFIX->value . 'seo_' . $personData->slug,
|
||||
now()->addHours(24),
|
||||
fn() => $this->seoPageProvider->getSeoForModel($personData)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,38 +2,22 @@
|
||||
|
||||
namespace App\Containers\User\UI\WEB\Controllers;
|
||||
|
||||
use App\Containers\User\Actions\GetPersonDataAction;
|
||||
use App\Containers\User\Models\User;
|
||||
use App\Containers\User\UI\WEB\Transformers\FullInfoPersonResource;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class PersonController extends Controller
|
||||
{
|
||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||
public function __construct(private readonly GetPersonDataAction $getPersonDataAction){}
|
||||
|
||||
public function show(string $slug)
|
||||
{
|
||||
$personData = Cache::remember(
|
||||
CacheKeys::USER_PREFIX->value . $slug,
|
||||
now()->addHours(24),
|
||||
fn() => User::with(['userDetail', 'departments_work.faculty', 'departments_teach.faculty', 'divisions', 'faculties'])
|
||||
->where('slug', $slug)
|
||||
->whereHas('userDetail')
|
||||
->firstOrFail()
|
||||
);
|
||||
|
||||
$seo = Cache::remember(
|
||||
CacheKeys::USER_PREFIX->value . 'seo_' . $slug,
|
||||
now()->addHours(24),
|
||||
fn() => $this->seoPageProvider->getSeoForModel($personData)
|
||||
);
|
||||
|
||||
$person = new FullInfoPersonResource($personData);
|
||||
|
||||
$data = $this->getPersonDataAction->run($slug);
|
||||
|
||||
$person = new FullInfoPersonResource($data['personData']);
|
||||
$seo = $data['seo'];
|
||||
|
||||
return Inertia::render('Client/Persons/Show', compact('person', 'seo'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user