From 5119dd72084f481298a6c591ce21cb29c143b5f1 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 22 Jun 2026 12:08:20 +0700 Subject: [PATCH] refactor: encapsulate password update and push subscription logic within dedicated services --- .../Admin/Account/PasswordController.php | 13 +++++-- .../PushSubscriptionController.php | 30 ++++++--------- app/Services/Account/ProfileService.php | 10 +++++ .../System/PushSubscriptionService.php | 37 +++++++++++++++++++ 4 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 app/Services/System/PushSubscriptionService.php diff --git a/app/Http/Controllers/Admin/Account/PasswordController.php b/app/Http/Controllers/Admin/Account/PasswordController.php index c525598..4ca3c9f 100644 --- a/app/Http/Controllers/Admin/Account/PasswordController.php +++ b/app/Http/Controllers/Admin/Account/PasswordController.php @@ -5,8 +5,8 @@ use App\Http\Controllers\Concerns\FlashesEntityMessage; use App\Http\Controllers\Controller; use App\Http\Requests\Admin\Account\UpdatePasswordRequest; +use App\Services\Account\ProfileService; use Illuminate\Http\RedirectResponse; -use Illuminate\Support\Facades\Hash; use Inertia\Inertia; use Inertia\Response; @@ -14,6 +14,10 @@ class PasswordController extends Controller { use FlashesEntityMessage; + public function __construct( + private readonly ProfileService $profileService, + ) {} + public function edit(): Response { return Inertia::render('admin/account/Password'); @@ -21,9 +25,10 @@ public function edit(): Response public function update(UpdatePasswordRequest $request): RedirectResponse { - $user = $request->user(); - $user->password = Hash::make($request->validated('password')); - $user->save(); + $this->profileService->updatePassword( + $request->user(), + $request->validated('password'), + ); $this->flashUpdated('Kata sandi'); diff --git a/app/Http/Controllers/PushSubscriptionController.php b/app/Http/Controllers/PushSubscriptionController.php index bea04ad..bff2c97 100644 --- a/app/Http/Controllers/PushSubscriptionController.php +++ b/app/Http/Controllers/PushSubscriptionController.php @@ -3,25 +3,20 @@ namespace App\Http\Controllers; use App\Http\Requests\PushSubscriptionRequest; -use App\Models\PushSubscription; +use App\Services\System\PushSubscriptionService; use Illuminate\Http\JsonResponse; -use Illuminate\Support\Facades\Auth; class PushSubscriptionController extends Controller { + public function __construct( + private readonly PushSubscriptionService $pushSubscriptionService, + ) {} + public function store(PushSubscriptionRequest $request): JsonResponse { - $validated = $request->validated(); - - PushSubscription::updateOrCreate( - [ - 'user_id' => Auth::id(), - 'endpoint' => $validated['endpoint'], - ], - [ - 'public_key' => $validated['publicKey'], - 'auth_token' => $validated['authToken'], - ] + $this->pushSubscriptionService->updateOrCreateSubscription( + $request->validated(), + $request->user()->id, ); return response()->json(['message' => 'Subscription berhasil disimpan.']); @@ -29,11 +24,10 @@ public function store(PushSubscriptionRequest $request): JsonResponse public function destroy(PushSubscriptionRequest $request): JsonResponse { - $validated = $request->validated(); - - PushSubscription::where('user_id', Auth::id()) - ->where('endpoint', $validated['endpoint']) - ->delete(); + $this->pushSubscriptionService->deleteSubscription( + $request->validated('endpoint'), + $request->user()->id, + ); return response()->json(['message' => 'Subscription berhasil dihapus.']); } diff --git a/app/Services/Account/ProfileService.php b/app/Services/Account/ProfileService.php index b48f48e..976dd1b 100644 --- a/app/Services/Account/ProfileService.php +++ b/app/Services/Account/ProfileService.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Hash; class ProfileService { @@ -29,4 +30,13 @@ public function update(array $validated, User $user): void ); }); } + + /** + * Update user password. + */ + public function updatePassword(User $user, string $password): void + { + $user->password = Hash::make($password); + $user->save(); + } } diff --git a/app/Services/System/PushSubscriptionService.php b/app/Services/System/PushSubscriptionService.php new file mode 100644 index 0000000..731267e --- /dev/null +++ b/app/Services/System/PushSubscriptionService.php @@ -0,0 +1,37 @@ + $validated + */ + public function updateOrCreateSubscription(array $validated, int $userId): void + { + PushSubscription::updateOrCreate( + [ + 'user_id' => $userId, + 'endpoint' => $validated['endpoint'], + ], + [ + 'public_key' => $validated['publicKey'], + 'auth_token' => $validated['authToken'], + ] + ); + } + + /** + * Delete a push subscription. + */ + public function deleteSubscription(string $endpoint, int $userId): void + { + PushSubscription::where('user_id', $userId) + ->where('endpoint', $endpoint) + ->delete(); + } +}