Get rid of UserRepository in favor of more specific actions
ADD
app/Actions/CreateUserAction.php
+<?php
+
+namespace App\Actions;
+
+use App\Models\User;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Str;
+
+class CreateUserAction
+{
+ public function execute(string $name, string $email, string $password): User
+ {
+ return User::create([
+ 'name' => $name,
+ 'email' => $email,
+ 'password' => Hash::make($password),
+ 'verification_token' => Str::random(100)
+ ]);
+ }
+}

ADD
app/Actions/VerifyUserAction.php
+<?php
+
+namespace App\Actions;
+
+use App\Exceptions\UserNotFoundException;
+use App\Models\User;
+
+class VerifyUserAction
+{
+ public function execute(int $id): void
+ {
+ $user = User::find($id);
+
+ if (!$user) {
+ throw new UserNotFoundException();
+ }
+
+ $user->update(['verification_token' => null]);
+ }
+}

MODIFY
app/Http/Controllers/LoginController.php
app/Http/Controllers/LoginController.php
namespace App\Http\Controllers;
use App\Actions\StoreSpaceInSessionAction;
+use App\Models\User;
use App\Repositories\LoginAttemptRepository;
-use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
- private $userRepository;
private $loginAttemptRepository;
- public function __construct(UserRepository $userRepository, LoginAttemptRepository $loginAttemptRepository)
+ public function __construct(LoginAttemptRepository $loginAttemptRepository)
{
- $this->userRepository = $userRepository;
$this->loginAttemptRepository = $loginAttemptRepository;
}
@@ -41,7 +39,7 @@ class LoginController extends Controller
return redirect()->route('dashboard');
} else {
if ($request->input('email')) {
- $user = $this->userRepository->getByEmail($request->input('email'));
+ $user = User::where('email', $request->input('email'))->first();
$this->loginAttemptRepository->create($user ? $user->id : null, $request->ip(), true);
}

MODIFY
app/Http/Controllers/RegisterController.php
app/Http/Controllers/RegisterController.php
namespace App\Http\Controllers;
+use App\Actions\CreateUserAction;
use App\Actions\StoreSpaceInSessionAction;
use App\Actions\SendVerificationMailAction;
use App\Models\Currency;
use App\Models\LoginAttempt;
+use App\Models\User;
use Illuminate\Http\Request;
// use App\Http\Controllers\Controller;
use App\Repositories\LoginAttemptRepository;
use App\Repositories\SpaceRepository;
-use App\Repositories\UserRepository;
use Illuminate\Support\Facades\Auth;
class RegisterController extends Controller
{
- private $userRepository;
private $spaceRepository;
private $loginAttemptRepository;
public function __construct(
- UserRepository $userRepository,
SpaceRepository $spaceRepository,
LoginAttemptRepository $loginAttemptRepository
) {
- $this->userRepository = $userRepository;
$this->spaceRepository = $spaceRepository;
$this->loginAttemptRepository = $loginAttemptRepository;
}
@@ -39,9 +37,9 @@ class RegisterController extends Controller
public function store(Request $request)
{
- $request->validate($this->userRepository->getValidationRulesForRegistration());
+ $request->validate(User::getValidationRulesForRegistration());
- $user = $this->userRepository->create($request->name, $request->email, $request->password);
+ $user = (new CreateUserAction())->execute($request->name, $request->email, $request->password);
$space = $this->spaceRepository->create($request->currency, $user->name . '\'s Space');
$user->spaces()->attach($space->id, ['role' => 'admin']);

MODIFY
app/Http/Controllers/ResetPasswordController.php
app/Http/Controllers/ResetPasswordController.php
namespace App\Http\Controllers;
use App\Mail\ResetPassword;
+use App\Models\User;
use App\Repositories\PasswordResetRepository;
-use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
@@ -12,12 +12,10 @@ use Illuminate\Support\Str;
class ResetPasswordController extends Controller
{
- private $userRepository;
private $passwordResetRepository;
- public function __construct(UserRepository $userRepository, PasswordResetRepository $passwordResetRepository)
+ public function __construct(PasswordResetRepository $passwordResetRepository)
{
- $this->userRepository = $userRepository;
$this->passwordResetRepository = $passwordResetRepository;
}
@@ -30,12 +28,12 @@ class ResetPasswordController extends Controller
public function post(Request $request)
{
- $request->validate($this->userRepository->getValidationRulesForPasswordReset());
+ $request->validate(User::getValidationRulesForPasswordReset());
if ($request->input('email') && !$request->has('token')) {
$email = $request->input('email');
- $existingUser = $this->userRepository->getByEmail($email);
+ $existingUser = User::where('email', $email)->first();
if ($existingUser) {
$shippingToken = null;
@@ -66,9 +64,9 @@ class ResetPasswordController extends Controller
$record = $this->passwordResetRepository->getByToken($token);
if ($record) {
- $user = $this->userRepository->getByEmail($record->email);
+ $user = User::where('email', $record->email)->first();
- $this->userRepository->update($user->id, ['password' => Hash::make($password)]);
+ $user->update(['password' => Hash::make($password)]);
}
$this->passwordResetRepository->delete($token);

MODIFY
app/Http/Controllers/VerifyController.php
app/Http/Controllers/VerifyController.php
namespace App\Http\Controllers;
-use App\Repositories\UserRepository;
+use App\Actions\VerifyUserAction;
+use App\Models\User;
use Illuminate\Http\Request;
class VerifyController extends Controller
{
- private $userRepository;
-
- public function __construct(UserRepository $userRepository)
- {
- $this->userRepository = $userRepository;
- }
-
public function __invoke($token)
{
- $user = $this->userRepository->getByVerificationToken($token);
+ $user = User::where('verification_token', $token)->first();
if (!$user) {
return redirect()->route('login');
}
- $this->userRepository->verifyById($user->id);
+ (new VerifyUserAction())->execute($user->id);
return redirect()
->route('login')

MODIFY
app/Models/User.php
app/Models/User.php
@@ -29,6 +29,25 @@ class User extends Authenticatable
'plan' => 'standard'
];
+ //
+ public static function getValidationRulesForRegistration(): array
+ {
+ return [
+ 'name' => 'required',
+ 'email' => 'required|email|unique:users',
+ 'password' => 'required|confirmed',
+ 'currency' => 'required|exists:currencies,id'
+ ];
+ }
+
+ public static function getValidationRulesForPasswordReset(): array
+ {
+ return [
+ 'email' => 'required_without:password|email',
+ 'password' => 'required_without:email|confirmed'
+ ];
+ }
+
// Accessors
public function getAvatarAttribute($avatar)
{

DELETE
app/Repositories/UserRepository.php
-<?php
-
-namespace App\Repositories;
-
-use App\Models\User;
-use Exception;
-use Illuminate\Support\Facades\Hash;
-use Illuminate\Support\Str;
-
-class UserRepository
-{
- public function getValidationRulesForRegistration(): array
- {
- return [
- 'name' => 'required',
- 'email' => 'required|email|unique:users',
- 'password' => 'required|confirmed',
- 'currency' => 'required|exists:currencies,id'
- ];
- }
-
- public function getValidationRulesForPasswordReset(): array
- {
- return [
- 'email' => 'required_without:password|email',
- 'password' => 'required_without:email|confirmed'
- ];
- }
-
- public function getById(int $id): ?User
- {
- return User::where('id', $id)->first();
- }
-
- public function getByEmail(string $email): ?User
- {
- return User::where('email', $email)->first();
- }
-
- public function getByVerificationToken(string $token): ?User
- {
- return User::where('verification_token', $token)->first();
- }
-
- public function verifyById(int $id): void
- {
- $user = $this->getById($id);
-
- if (!$user) {
- throw new Exception('User (ID ' . $id . ') not found');
- }
-
- $user->update([
- 'verification_token' => null
- ]);
- }
-
- public function create(string $name, string $email, string $password): User
- {
- return User::create([
- 'name' => $name,
- 'email' => $email,
- 'password' => Hash::make($password),
- 'verification_token' => Str::random(100)
- ]);
- }
-
- public function update(int $id, array $data): void
- {
- $user = $this->getById($id);
-
- if (!$user) {
- throw new Exception('Could not find user with ID ' . $id);
- }
-
- $user->fill($data)->save();
- }
-}

Get rid of UserRepository in favor of more specific actions