Skip to content

Commit eb05f68

Browse files
committed
refactor: clean and modularize some code
1 parent 94653c3 commit eb05f68

22 files changed

+195
-67
lines changed

app/Actions/Notes/StoreNoteAction.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
use App\Dtos\Notes\StoreNoteData;
66
use App\Dtos\Subscriptions\SubscriptionRulesData;
77
use App\Exceptions\NoteExceptions;
8+
use App\Exceptions\SubscriptionExceptions;
89
use App\Models\Note;
910
use App\Models\User;
1011

1112
class StoreNoteAction
1213
{
1314
/**
1415
* @throws NoteExceptions
16+
* @throws SubscriptionExceptions
1517
*/
1618
public function handle(StoreNoteData $data, User $user): Note
1719
{
1820
$userSubscription = $user->subscription;
1921

2022
if ($userSubscription === null) {
21-
throw NoteExceptions::userDoesNotHaveSubscription();
23+
throw SubscriptionExceptions::userDoesNotHaveSubscription();
2224
}
2325

2426
$notesAmount = $user->notes()->count();

app/Exceptions/NoteExceptions.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,4 @@ public static function notesAmountLimit(int $limitNotes): self
1010
{
1111
return new static(__('validation.custom.notes.limit', ['amount' => $limitNotes]));
1212
}
13-
14-
public static function userDoesNotHaveSubscription(): self
15-
{
16-
return new static(__('validation.custom.subscription.required'));
17-
}
1813
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class SubscriptionExceptions extends Exception
8+
{
9+
public static function userDoesNotHaveSubscription(): self
10+
{
11+
return new static(__('validation.custom.subscription.required'));
12+
}
13+
}

app/Http/Controllers/Api/NotesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Dtos\Notes\StoreNoteData;
88
use App\Dtos\Notes\UpdateNoteData;
99
use App\Exceptions\NoteExceptions;
10+
use App\Exceptions\SubscriptionExceptions;
1011
use App\Http\Controllers\Controller;
1112
use App\Http\Controllers\NoteResource;
1213
use App\Http\Requests\UpdateNoteRequest;
@@ -44,7 +45,7 @@ public function store(StoreNoteRequest $request): JsonResponse
4445

4546
try {
4647
$note = (new StoreNoteAction())->handle($data, $authUser);
47-
} catch (NoteExceptions $e) {
48+
} catch (NoteExceptions|SubscriptionExceptions $e) {
4849
return response()->json(['message' => $e->getMessage()], 400);
4950
}
5051

app/Http/Controllers/NotesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Dtos\Notes\StoreNoteData;
88
use App\Dtos\Notes\UpdateNoteData;
99
use App\Exceptions\NoteExceptions;
10+
use App\Exceptions\SubscriptionExceptions;
1011
use App\Http\Requests\UpdateNoteRequest;
1112
use App\Models\Note;
1213
use App\Models\User;
@@ -49,7 +50,7 @@ public function store(StoreNoteRequest $request): RedirectResponse
4950

5051
try {
5152
(new StoreNoteAction())->handle($data, $authUser);
52-
} catch (NoteExceptions $e) {
53+
} catch (NoteExceptions|SubscriptionExceptions $e) {
5354
return redirect()
5455
->route('notes.index')
5556
->with('warning', $e->getMessage());

tests/Feature/Http/Controllers/Api/Notes/DestroyNoteControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use App\Models\User;
66
use App\Models\Note;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
8-
use Illuminate\Foundation\Testing\WithFaker;
98
use Tests\TestCase;
109

1110
class DestroyNoteControllerTest extends TestCase
1211
{
1312
use RefreshDatabase;
14-
use WithFaker;
1513

1614
private string $url = 'api/notes';
1715

tests/Feature/Http/Controllers/Api/Notes/IndexNotesControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use App\Models\User;
66
use App\Models\Note;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
8-
use Illuminate\Foundation\Testing\WithFaker;
98
use Tests\TestCase;
109

1110
class IndexNotesControllerTest extends TestCase
1211
{
1312
use RefreshDatabase;
14-
use WithFaker;
1513

1614
private string $url = 'api/notes';
1715

tests/Feature/Http/Controllers/Api/Notes/ShowNoteControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
use App\Models\User;
66
use App\Models\Note;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
8-
use Illuminate\Foundation\Testing\WithFaker;
98
use Illuminate\Testing\Fluent\AssertableJson;
109
use Tests\TestCase;
1110

1211
class ShowNoteControllerTest extends TestCase
1312
{
1413
use RefreshDatabase;
15-
use WithFaker;
1614

1715
private string $url = 'api/notes';
1816

tests/Feature/Http/Controllers/Api/Notes/StoreNoteControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use App\Models\Subscription;
66
use App\Models\User;
77
use Illuminate\Foundation\Testing\RefreshDatabase;
8-
use Illuminate\Foundation\Testing\WithFaker;
98
use Tests\TestCase;
109

1110
class StoreNoteControllerTest extends TestCase
1211
{
1312
use RefreshDatabase;
14-
use WithFaker;
1513

1614
private string $url = 'api/notes';
1715

tests/Feature/Http/Controllers/Api/Notes/StoreNoteValidationsControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
use App\Models\Subscription;
77
use App\Models\User;
88
use Illuminate\Foundation\Testing\RefreshDatabase;
9-
use Illuminate\Foundation\Testing\WithFaker;
109
use Tests\TestCase;
1110

1211
class StoreNoteValidationsControllerTest extends TestCase
1312
{
1413
use RefreshDatabase;
15-
use WithFaker;
1614

1715
private string $url = 'api/notes';
1816

0 commit comments

Comments
 (0)