Move database interactions away from LoginController and RegisterController
MODIFY
app/Http/Controllers/LoginController.php
app/Http/Controllers/LoginController.php
namespace App\Http\Controllers;
-use App\Models\LoginAttempt;
-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)
+ {
+ $this->userRepository = $userRepository;
+ $this->loginAttemptRepository = $loginAttemptRepository;
+ }
+
public function index() {
return view('login');
}
@@ -19,23 +28,15 @@ class LoginController extends Controller {
])) {
$user = Auth::user();
- LoginAttempt::create([
- 'user_id' => $user->id,
- 'ip' => $request->ip(),
- 'failed' => false
- ]);
+ $this->loginAttemptRepository->create($user->id, $request->ip(), false);
session(['space' => $user->spaces[0]]);
return redirect()->route('dashboard');
} else {
- $user = User::where('email', $request->input('email'))->first();
+ $user = $this->userRepository->getByEmail($request->input('email'));
- LoginAttempt::create([
- 'user_id' => $user ? $user->id : null,
- 'ip' => $request->ip(),
- 'failed' => true
- ]);
+ $this->loginAttemptRepository->create($user ? $user->id : null, $request->ip(), true);
$request->flash();

MODIFY
app/Http/Controllers/RegisterController.php
app/Http/Controllers/RegisterController.php
@@ -7,62 +7,45 @@ use Illuminate\Http\Request;
// use App\Http\Controllers\Controller;
use App\Mail\VerifyRegistration;
-use App\Models\Currency;
-use App\Models\User;
-use App\Models\Space;
-use Hash;
+use App\Repositories\CurrencyRepository;
+use App\Repositories\LoginAttemptRepository;
+use App\Repositories\SpaceRepository;
+use App\Repositories\UserRepository;
use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Str;
use Mail;
class RegisterController extends Controller {
- public function index() {
- $currencies = [];
-
- foreach (Currency::all() as $currency) {
- $currencies[] = ['key' => $currency->id, 'label' => $currency->symbol];
+ private $currencyRepository;
+ private $userRepository;
+ private $spaceRepository;
+ private $loginAttemptRepository;
+
+ public function __construct(CurrencyRepository $currencyRepository, UserRepository $userRepository, SpaceRepository $spaceRepository, LoginAttemptRepository $loginAttemptRepository)
+ {
+ $this->currencyRepository = $currencyRepository;
+ $this->userRepository = $userRepository;
+ $this->spaceRepository = $spaceRepository;
+ $this->loginAttemptRepository = $loginAttemptRepository;
}
- return view('register', compact('currencies'));
+ public function index() {
+ return view('register', [
+ 'currencies' => $this->currencyRepository->getKeyValueArray()
+ ]);
}
public function store(Request $request) {
- $request->validate([
- 'name' => 'required',
- 'email' => 'required|email|unique:users',
- 'password' => 'required|confirmed',
- 'currency' => 'required|exists:currencies,id'
- ]);
-
- // User
- $user = new User;
-
- $user->name = $request->name;
- $user->email = $request->email;
- $user->password = Hash::make($request->password);
- $user->verification_token = Str::random(100);
-
- $user->save();
-
- // Space
- $space = new Space;
-
- $space->currency_id = $request->currency;
- $space->name = $user->name . '\'s Space';
-
- $space->save();
+ $request->validate($this->userRepository->getValidationRulesForRegistration());
+ $user = $this->userRepository->create($request->name, $request->email, $request->password);
+ $space = $this->spaceRepository->create($request->currency, $user->name . '\'s Space');
$user->spaces()->attach($space->id, ['role' => 'admin']);
Mail::to($user->email)->queue(new VerifyRegistration($user));
Auth::loginUsingId($user->id);
- LoginAttempt::create([
- 'user_id' => $user->id,
- 'ip' => $request->ip(),
- 'failed' => false
- ]);
+ $this->loginAttemptRepository->create($user->id, $request->ip(), false);
session(['space' => $user->spaces[0]]);

MODIFY
app/Models/Space.php
app/Models/Space.php
@@ -9,6 +9,11 @@ use Illuminate\Support\Facades\DB;
class Space extends Model {
use SoftDeletes;
+ protected $fillable = [
+ 'currency_id',
+ 'name'
+ ];
+
protected $dates = ['deleted_at'];
// Relations

ADD
app/Repositories/CurrencyRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\Currency;
+
+class CurrencyRepository
+{
+ public function getAll()
+ {
+ return Currency::all();
+ }
+
+ public function getKeyValueArray(): array
+ {
+ $response = [];
+
+ foreach ($this->getAll() as $currency) {
+ $response[] = [
+ 'key' => $currency->id,
+ 'label' => $currency->symbol
+ ];
+ }
+
+ return $response;
+ }
+}

ADD
app/Repositories/LoginAttemptRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\LoginAttempt;
+
+class LoginAttemptRepository
+{
+ public function create(?int $userId, string $ip, bool $failed): LoginAttempt
+ {
+ return LoginAttempt::create([
+ 'user_id' => $userId,
+ 'ip' => $ip,
+ 'failed' => $failed
+ ]);
+ }
+}

ADD
app/Repositories/SpaceRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\Space;
+
+class SpaceRepository
+{
+ public function create(int $currencyId, string $name): Space
+ {
+ return Space::create([
+ 'currency_id' => $currencyId,
+ 'name' => $name
+ ]);
+ }
+}

MODIFY
app/Repositories/UserRepository.php
app/Repositories/UserRepository.php
@@ -4,14 +4,31 @@ 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 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();
@@ -29,4 +46,14 @@ class UserRepository
'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)
+ ]);
+ }
}

Move database interactions away from LoginController and RegisterController