Skip to content

Commit bc721e4

Browse files
committed
refactor: (LAR-86) refactoring test and translation 🥲
1 parent 4bd49e1 commit bc721e4

21 files changed

+194
-147
lines changed

‎app/Actions/Article/CreateArticleAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public function execute(CreateArticleData $articleData): Article
3737

3838
return $article;
3939
}
40-
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
use App\Events\UserBannedEvent;
7+
8+
final class BanUserAction
9+
{
10+
public function execute(User $user, string $reason): void
11+
{
12+
if($user->banned_at == null) {
13+
$user->banned_at = now();
14+
$user->banned_reason = $reason;
15+
$user->save();
16+
17+
event(new UserBannedEvent($user));
18+
}
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
use App\Events\UserUnbannedEvent;
7+
8+
final class UnBanUserAction
9+
{
10+
public function execute(User $user) : void
11+
{
12+
$user->banned_at = null;
13+
$user->banned_reason = null;
14+
$user->save();
15+
16+
event(new UserUnbannedEvent($user));
17+
}
18+
}

‎app/Events/UserBannedEvent.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,4 @@ final class UserBannedEvent
1818
use Dispatchable, InteractsWithSockets, SerializesModels;
1919

2020
public function __construct(public User $user){}
21-
22-
/**
23-
* Get the channels the event should broadcast on.
24-
*
25-
* @return array<int, \Illuminate\Broadcasting\Channel>
26-
*/
27-
public function broadcastOn(): array
28-
{
29-
return [
30-
new PrivateChannel('user-banned'),
31-
];
32-
}
3321
}

‎app/Events/UserUnbannedEvent.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,4 @@ final class UserUnbannedEvent
1818
use Dispatchable, InteractsWithSockets, SerializesModels;
1919

2020
public function __construct(public User $user){}
21-
22-
/**
23-
* Get the channels the event should broadcast on.
24-
*
25-
* @return array<int, \Illuminate\Broadcasting\Channel>
26-
*/
27-
public function broadcastOn(): array
28-
{
29-
return [
30-
new PrivateChannel('unban-user'),
31-
];
32-
}
3321
}

‎app/Filament/Resources/UserResource.php

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use App\Events\UserBannedEvent;
1212
use Filament\Resources\Resource;
1313
use App\Events\UserUnbannedEvent;
14+
use App\Actions\User\BanUserAction;
15+
use App\Actions\User\UnBanUserAction;
1416
use Filament\Forms\Components\TextInput;
1517
use Filament\Notifications\Notification;
1618
use Illuminate\Database\Eloquent\Builder;
@@ -56,16 +58,16 @@ public static function table(Table $table): Table
5658
->icon('untitledui-inbox')
5759
->description(fn ($record): ?string => $record->phone_number),
5860
Tables\Columns\TextColumn::make('email_verified_at')
59-
->label(__('global.ban.label.validate_email'))
61+
->label(__('user.validate_email'))
6062
->placeholder('N/A')
6163
->date(),
6264
Tables\Columns\TextColumn::make(name: 'created_at')
63-
->label(__('global.ban.label.inscription'))
65+
->label(__('use.inscription'))
6466
->date(),
6567
])
6668
->filters([
6769
Tables\Filters\TernaryFilter::make('email_verified_at')
68-
->label(__('global.ban.label.email_verified'))
70+
->label(__('user.email_verified'))
6971
->nullable(),
7072
])
7173
->actions([
@@ -74,26 +76,44 @@ public static function table(Table $table): Table
7476
->icon('untitledui-archive')
7577
->color('warning')
7678
->visible(fn ($record) => $record->banned_at == null)
77-
->modalHeading(__('global.ban.heading'))
78-
->modalDescription(__('global.ban.description'))
79+
->modalHeading(__('user.ban.heading'))
80+
->modalDescription(__('user.ban.description'))
7981
->form([
8082

8183
TextInput::make('banned_reason')
82-
->label(__('global.ban.reason'))
84+
->label(__('user.ban.reason'))
8385
->required(),
8486
])
8587
->action(function (User $record, array $data) {
8688
if (!self::canBanUser($record)) {
8789
Notification::make()
8890
->warning()
89-
->title(__('notifications.user.cannot.title'))
90-
->body(__('notifications.user.cannot.ban_admin'))
91+
->title(__('notifications.user.cannot_ban_title'))
92+
->body(__('notifications.user.cannot_ban_admin'))
9193
->duration(5000)
9294
->send();
9395

9496
return;
9597
}
96-
self::BanUserAction($record, $data['banned_reason']);
98+
99+
if ($record->banned_at !== null) {
100+
Notification::make()
101+
->warning()
102+
->title(__('notifications.user.cannot_ban_title'))
103+
->body(__('notifications.user.cannot_ban_body'))
104+
->send();
105+
106+
return;
107+
}
108+
109+
app(BanUserAction::class)->execute($record, $data['banned_reason']);
110+
111+
Notification::make()
112+
->success()
113+
->duration(5000)
114+
->title(__('notifications.user.banned_title'))
115+
->body(__('notifications.user.banned_body'))
116+
->send();
97117
})
98118
->requiresConfirmation(),
99119

@@ -103,7 +123,14 @@ public static function table(Table $table): Table
103123
->color('success')
104124
->visible(fn ($record) => $record->banned_at !== null)
105125
->action(function (User $record) {
106-
self::UnbanUserAction($record);
126+
app(UnBanUserAction::class)->execute($record);
127+
128+
Notification::make()
129+
->success()
130+
->title(__('notifications.user.unbanned_title'))
131+
->duration(5000)
132+
->body(__('notifications.user.unbanned_body'))
133+
->send();
107134
})
108135
->requiresConfirmation(),
109136

@@ -121,48 +148,6 @@ public static function getPages(): array
121148
];
122149
}
123150

124-
public static function BanUserAction(User $record, $reason): void
125-
{
126-
if ($record->banned_at !== null) {
127-
Notification::make()
128-
->warning()
129-
->title(__('notifications.user.cannot.title'))
130-
->body(__('notifications.user.cannot.body'))
131-
->send();
132-
133-
return;
134-
}
135-
136-
$record->banned_at = Carbon::now();
137-
$record->banned_reason = $reason;
138-
$record->save();
139-
140-
Notification::make()
141-
->success()
142-
->duration(5000)
143-
->title(__('notifications.user.banned.title'))
144-
->body(__('notifications.user.banned.body'))
145-
->send();
146-
147-
event(new UserBannedEvent($record));
148-
}
149-
150-
public static function UnbanUserAction(User $record): void
151-
{
152-
$record->banned_at = null;
153-
$record->banned_reason = null;
154-
$record->save();
155-
156-
Notification::make()
157-
->success()
158-
->title(__('notifications.user.unbanned.title'))
159-
->duration(5000)
160-
->body(__('notifications.user.unbanned.body'))
161-
->send();
162-
163-
event(new UserUnbannedEvent($record));
164-
}
165-
166151
public static function canBanUser(User $record): bool
167152
{
168153
return !$record->hasRole('admin');

‎app/Filament/Resources/UserResource/Pages/ListUsers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ final class ListUsers extends ListRecords
1515
public function getTabs() : array
1616
{
1717
return [
18-
__('global.ban.all') => Tab::make(__('global.ban.all')),
19-
__('global.ban.banned') => Tab::make(__('global.ban.banned'))
18+
__('global.all') => Tab::make(__('global.all')),
19+
__('global.banned') => Tab::make(__('global.banned'))
2020
->modifyQueryUsing(function ($query) {
2121
return $query->isBanned();
2222
}),
23-
__('global.ban.not_banned') => Tab::make(__('global.ban.not_banned'))
23+
__('global.unbanned') => Tab::make(__('global.unbanned'))
2424
->modifyQueryUsing(function ($query) {
2525
return $query->isNotBanned();
2626
}),

‎app/Mail/UserBannedEMail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public function __construct(public User $user){}
2121
public function envelope(): Envelope
2222
{
2323
return new Envelope(
24-
subject: __('global.ban.ban_email_subject'),
24+
subject: __('user.ban.email_subject'),
2525
);
2626
}
2727

2828
public function content(): Content
2929
{
3030
return new Content(
31-
markdown: 'emails.send-user-banned-message',
31+
markdown: 'emails.send-banned-message',
3232
);
3333
}
3434
}

‎app/Mail/UserUnBannedEMail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public function __construct(public User $user){}
2121
public function envelope(): Envelope
2222
{
2323
return new Envelope(
24-
subject: __('global.ban.unban_email_subject'),
24+
subject: __('user.unbanned.email_subject'),
2525
);
2626
}
2727

2828
public function content(): Content
2929
{
3030
return new Content(
31-
markdown: 'emails.send-user-unbanned-message',
31+
markdown: 'emails.send-unbanned-message',
3232
);
3333
}
3434
}

‎database/migrations/2024_11_10_032051_add_ban_field_to_users_table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function up(): void
1919
public function down(): void
2020
{
2121
Schema::table('users', function (Blueprint $table) {
22-
$table->dropColumn('banned_at');
23-
$table->dropColumn('banned_reason');
22+
$table->dropColumn(['banned_at','banned_reason']);
2423
});
2524
}
2625
};

‎lang/en/global.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,8 @@
9090
'discussion_description' => 'Discuss and debate different themes and ideas',
9191
],
9292
'moderator' => 'Moderator',
93-
'ban' => [
94-
'reason' => 'Reason for banning',
95-
'heading' => 'Ban the user',
96-
'description' => 'Please enter the reason for banning.',
97-
'all' => 'All',
98-
'banned' => 'Banned',
99-
'not_banned' => 'Not banned',
100-
'ban_email_subject' => 'Laravelcm ban notification',
101-
'unban_email_subject' => 'Laravelcm unbanning notification',
102-
'message' => 'Your account has been banned. Contact the administrator for more information.',
103-
'label' => [
104-
'email_verified' => 'Email verified',
105-
'inscription' => 'Inscription',
106-
'validate_email' => 'Validation Email'
107-
],
108-
],
93+
'all' => 'All',
94+
'banned' => 'Banned',
95+
'unbanned' => 'Unbanned',
96+
10997
];

‎lang/en/notifications.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,12 @@
3131
'error' => 'Oops! You\'ve got errors.',
3232

3333
'user' => [
34-
'banned' => [
35-
'title' => 'The user has been banned.',
36-
'body' => 'The user has been notified that he has been banned'
37-
],
38-
'unbanned' => [
39-
'title' => 'The user has been un-banned',
40-
'body' => 'The user has been notified that he can log in again.'
41-
],
42-
'cannot' => [
43-
'title' => 'Unable to ban',
44-
'body' => 'This user is already banned.',
45-
'ban_admin' => 'You cannot ban an administrator.',
46-
]
34+
'banned_title' => 'The user has been banned.',
35+
'banned_body' => 'The user has been notified that he has been banned',
36+
'unbanned_title' => 'The user has been un-banned',
37+
'unbanned_body' => 'The user has been notified that he can log in again.',
38+
'cannot_ban_title' => 'Unable to ban',
39+
'cannot_ban_body' => 'This user is already banned.',
40+
'cannot_ban_admin' => 'You cannot ban an administrator.',
4741
]
4842
];

‎lang/en/user.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
7+
'email_verified' => 'Email verified',
8+
'inscription' => 'Inscription',
9+
'validate_email' => 'Validation Email',
10+
'ban' => [
11+
'reason' => 'Reason for banning',
12+
'heading' => 'Ban the user',
13+
'description' => 'Please enter the reason for banning.',
14+
'all' => 'All',
15+
'banned' => 'Banned',
16+
'not_banned' => 'Not banned',
17+
'email_subject' => 'Laravelcm ban notification',
18+
'message' => 'Your account has been banned. Contact the administrator for more information.',
19+
],
20+
'unbanned' => [
21+
'email_subject' => 'Laravelcm unbanning notification',
22+
]
23+
24+
];

‎lang/fr/global.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,8 @@
9090
'discussion_description' => 'Échangez, débattez sur différentes thématiques et idées.',
9191
],
9292
'moderator' => 'Modérateur',
93-
'ban' => [
94-
'reason' => 'Raison du bannissement',
95-
'heading' => 'Bannir l\'utilisateur',
96-
'description' => 'Veuillez entrer la raison du bannissement.',
97-
'all' => 'Tout',
98-
'banned' => 'Bannis',
99-
'not_banned' => 'Non bannis',
100-
'ban_email_subject' => 'Notification de bannissement Laravelcm',
101-
'unban_email_subject' => 'Notification de dé-baannissement Laravelcm',
102-
'message' => 'Votre compte a été banni. Contactez l\'administrateur pour plus d\'informations.',
103-
'label' => [
104-
'email_verified' => 'Email verified',
105-
'inscription' => 'Inscription',
106-
'validate_email' => 'Validation Email'
107-
],
108-
]
93+
'all' => 'Tout',
94+
'banned' => 'Bannis',
95+
'unbanned' => 'Non bannis',
10996

11097
];

0 commit comments

Comments
 (0)