Skip to content

Commit

Permalink
refactor: better action classes
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Sep 4, 2023
1 parent e9f11de commit b7df0d2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
13 changes: 6 additions & 7 deletions app/Actions/User/ChangePasswordAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

namespace App\Actions\User;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class ChangePasswordAction
{
public function execute($request): void
public function execute(User $user, $data): void
{
if (! Hash::check($request->old_password, $request->user()->password)) {
if (! Hash::check($data['old_password'], $user->password)) {
throw ValidationException::withMessages([
'old_password' => 'Old password is wrong!',
]);
}

$request
->user()
->update([
'password' => Hash::make($request->password)
]);
$user->update([
'password' => Hash::make($data['password'])
]);
}
}
12 changes: 6 additions & 6 deletions app/Actions/User/CreateUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

class CreateUserAction
{
public function execute($user)
public function execute(array $data): User
{
User::create([
'email' => $user->email,
'password' => Hash::make($user->password),
'nick_name' => $user->nick_name,
'country_id' => $user->country_id,
return User::create([
'email' => $data['email'],
'password' => Hash::make($data['password']),
'nick_name' => $data['nick_name'],
'country_id' => $data['country_id'],
'addiction_type' => AddictionType::Unknown,
]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/User/EditProfileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class EditProfileAction
{
public function execute(User $user, $fields): User
public function execute(User $user, array $data): User
{
$user->update($fields->validated());
$user->update($data);

return $user;
}
Expand Down
8 changes: 4 additions & 4 deletions app/Actions/User/LoginUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

class LoginUserAction
{
public function execute($request): string
public function execute(array $data): string
{
$user = User::firstWhere('email', $request->email);
$user = User::firstWhere('email', $data['email']);

if (! $user || ! Hash::check($request->password, $user->password)) {
if (! $user || ! Hash::check($data['password'], $user->password)) {
abort(401, 'User not found!');
}

return $user
->createToken($request->header('User-Agent'))
->createToken(request()->header('User-Agent', 'Unkown User Agent'))
->plainTextToken;
}
}
6 changes: 4 additions & 2 deletions app/Actions/User/LogoutUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Actions\User;

use App\Models\User;

class LogoutUserAction
{
public function execute($request): void
public function execute(User $user): void
{
$request->user()->currentAccessToken()->delete();
$user->currentAccessToken()->delete();
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public function __construct()

public function register(UserRegistrationRequest $request, CreateUserAction $createUserAction)
{
$createUserAction->execute($request);
$createUserAction->execute($request->validated());

return $this->created();
}

public function login(UserLoginRequest $request, LoginUserAction $loginUserAction)
{
$token = $loginUserAction->execute($request);
$token = $loginUserAction->execute($request->validated());

return $this->ok(
UserTokenResource::make($token)
Expand All @@ -37,14 +37,14 @@ public function login(UserLoginRequest $request, LoginUserAction $loginUserActio

public function logout(Request $request, LogoutUserAction $logoutUserAction)
{
$logoutUserAction->execute($request);
$logoutUserAction->execute($request->user());

return $this->ok();
}

public function changePassword(ChangePasswordRequest $request, ChangePasswordAction $changePasswordAction)
{
$changePasswordAction->execute($request);
$changePasswordAction->execute($request->user(), $request->validated());

return $this->ok();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function index(Request $request)

public function update(UpdateProfileRequest $request, EditProfileAction $editProfileAction)
{
$user = $editProfileAction->execute($request->user(), $request);
$user = $editProfileAction->execute($request->user(), $request->validated());

return $this->ok(
UserResource::make($user)
Expand Down

0 comments on commit b7df0d2

Please sign in to comment.