Skip to content

Commit cf3efdf

Browse files
committed
Implemented the repository pattern
1 parent 214c302 commit cf3efdf

File tree

10 files changed

+99
-30
lines changed

10 files changed

+99
-30
lines changed

app/Console/Commands/CreateUser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace App\Console\Commands;
66

7-
use App\Models\User;
7+
use App\Repository\UserRepositoryInterface;
88
use Illuminate\Console\Command;
9-
use Illuminate\Contracts\Hashing\Hasher;
109

1110
final class CreateUser extends Command
1211
{
@@ -22,12 +21,12 @@ public function handle(): int
2221
$email = $this->argument('Email');
2322
$password = $this->argument('Password');
2423

25-
$hasher = app()->get(Hasher::class);
24+
$userRepository = app()->get(UserRepositoryInterface::class);
2625

27-
User::factory()->createOne([
26+
$userRepository->create([
2827
'name' => $name,
2928
'email' => $email,
30-
'password' => $hasher->make($password)
29+
'password' => $password
3130
]);
3231

3332
$this->line('User created!');

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Http\Controllers\AbstractController;
88
use App\Models\User;
99
use App\Providers\RouteServiceProvider;
10+
use App\Repository\UserRepositoryInterface;
1011
use Illuminate\Contracts\Validation\Factory as Validation;
1112
use Illuminate\Contracts\Validation\Validator;
1213
use Illuminate\Foundation\Auth\RegistersUsers;
@@ -35,7 +36,8 @@ protected function validator(array $data): Validator
3536

3637
protected function create(array $data): User
3738
{
38-
return User::factory()->create([
39+
$userRepository = app()->get(UserRepositoryInterface::class);
40+
return $userRepository->create([
3941
'name' => $data['name'],
4042
'email' => $data['email'],
4143
'password' => $data['password']

app/Listeners/TestEventListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace App\Listeners;
66

7-
use App\Models\User;
7+
use App\Repository\UserRepositoryInterface;
88

99
final class TestEventListener
1010
{
1111
public function handle(): void
1212
{
13-
User::factory()->createOne();
13+
$userRepository = app()->get(UserRepositoryInterface::class);
14+
$userRepository->create();
1415
}
1516
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Providers;
6+
7+
use App\Repository\Eloquent\UserRepository;
8+
use App\Repository\UserRepositoryInterface;
9+
use Illuminate\Support\ServiceProvider;
10+
11+
final class RepositoryServiceProvider extends ServiceProvider
12+
{
13+
public function register(): void
14+
{
15+
app()->bind(UserRepositoryInterface::class, UserRepository::class);
16+
}
17+
18+
public function boot(): void
19+
{
20+
//
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository\Eloquent;
6+
7+
use App\Models\User;
8+
use App\Repository\UserRepositoryInterface;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
final class UserRepository implements UserRepositoryInterface
12+
{
13+
/**
14+
* @param array $attributes
15+
* @return User
16+
*/
17+
public function create(array $attributes = []): Model
18+
{
19+
return User::factory()->createOne($attributes);
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Foundation\Auth\User as BaseUser;
9+
10+
interface UserRepositoryInterface
11+
{
12+
/**
13+
* @param array $attributes
14+
* @return BaseUser
15+
*/
16+
public function create(array $attributes = []): Model;
17+
}

composer.lock

Lines changed: 18 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
App\Providers\AppServiceProvider::class,
6868
App\Providers\AuthServiceProvider::class,
6969
App\Providers\EventServiceProvider::class,
70+
App\Providers\RepositoryServiceProvider::class,
7071
App\Providers\RouteServiceProvider::class,
7172

7273
],

database/seeders/UserSeeder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace Database\Seeders;
66

7-
use App\Models\User;
7+
use App\Repository\UserRepositoryInterface;
88
use Illuminate\Database\Seeder;
99

1010
final class UserSeeder extends Seeder
1111
{
1212
public function run(): void
1313
{
14-
User::factory()->createOne([
14+
$userRepository = app()->get(UserRepositoryInterface::class);
15+
$userRepository->create([
1516
'name' => 'John Doe',
1617
'email' => 'john_doe@gmail.com',
1718
'password' => '123456'

tests/Functional/LaravelModuleCest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Events\TestEvent;
88
use App\Http\Controllers\TestController;
99
use App\Models\User;
10+
use App\Repository\UserRepositoryInterface;
1011
use App\Utils\Contracts\StringConverterInterface;
1112
use App\Utils\Repeat;
1213
use App\Utils\ToUppercase;
@@ -19,8 +20,9 @@ final class LaravelModuleCest
1920
{
2021
public function amLoggedAs(FunctionalTester $I)
2122
{
23+
$userRepository = app()->get(UserRepositoryInterface::class);
2224
/** @var array $user */
23-
$user = User::factory()->createOne();
25+
$user = $userRepository->create();
2426
$I->amLoggedAs($user);
2527
$I->amOnPage('/home');
2628
$I->see('You are logged in!');
@@ -85,8 +87,9 @@ public function disableMiddleware(FunctionalTester $I)
8587

8688
public function disableModelEvents(FunctionalTester $I)
8789
{
90+
$userRepository = app()->get(UserRepositoryInterface::class);
8891
/** @var User $user */
89-
$user = User::factory()->createOne([
92+
$user = $userRepository->create([
9093
'email' => 'john_doe@original.com',
9194
'password' => 'password',
9295
]);
@@ -366,7 +369,8 @@ public function seeNumRecords(FunctionalTester $I)
366369

367370
public function seeRecord(FunctionalTester $I)
368371
{
369-
User::factory()->createOne([
372+
$userRepository = app()->get(UserRepositoryInterface::class);
373+
$userRepository->create([
370374
'email' => 'jane_doe@gmail.com',
371375
'password' => '123456'
372376
]);

0 commit comments

Comments
 (0)