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

ADD
app/Repositories/UserRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\User;
+use Exception;
+
+class UserRepository
+{
+ public function getById(int $id): ?User
+ {
+ return User::where('id', $id)->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
+ ]);
+ }
+}

Move logic out of VerifyController