From 1ceac3b58367f26e82d80648ed1cba05d1cb9693 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 1 Feb 2024 10:28:31 +0100 Subject: [PATCH] Make Email model changeable from config --- config/filament-email.php | 2 ++ database/factories/EmailFactory.php | 8 ++++++-- src/Filament/Resources/EmailResource.php | 11 +++++++---- src/Listeners/FilamentEmailLogger.php | 5 ++++- src/Mail/ResendMail.php | 5 ++--- tests/EmailModelTest.php | 13 +++++++++---- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/config/filament-email.php b/config/filament-email.php index 2d0c2d6..1dcadf8 100644 --- a/config/filament-email.php +++ b/config/filament-email.php @@ -2,11 +2,13 @@ // config for RickDBCN/FilamentEmail use RickDBCN\FilamentEmail\Filament\Resources\EmailResource; +use RickDBCN\FilamentEmail\Models\Email; return [ 'resource' => [ 'class' => EmailResource::class, + 'model' => Email::class, 'group' => null, 'sort' => null, 'default_sort_column' => 'created_at', diff --git a/database/factories/EmailFactory.php b/database/factories/EmailFactory.php index 27edac7..5b87e0f 100644 --- a/database/factories/EmailFactory.php +++ b/database/factories/EmailFactory.php @@ -3,13 +3,17 @@ namespace RickDBCN\FilamentEmail\Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Config; use RickDBCN\FilamentEmail\Models\Email; class EmailFactory extends Factory { - protected $model = Email::class; + public function modelName(): string + { + return Config::get('filament-email.resource.model') ?? Email::class; + } - public function definition() + public function definition(): array { return [ 'from' => $this->faker->email(), diff --git a/src/Filament/Resources/EmailResource.php b/src/Filament/Resources/EmailResource.php index bc34fe8..6d6af45 100644 --- a/src/Filament/Resources/EmailResource.php +++ b/src/Filament/Resources/EmailResource.php @@ -23,8 +23,6 @@ class EmailResource extends Resource { - protected static ?string $model = Email::class; - protected static ?string $navigationIcon = 'heroicon-o-envelope'; protected static ?string $slug = 'emails'; @@ -44,6 +42,11 @@ public static function getNavigationSort(): ?int return Config::get('filament-email.resource.sort') ?? parent::getNavigationSort(); } + public static function getModel(): string + { + return Config::get('filament-email.resource.model') ?? Email::class; + } + public static function form(Form $form): Form { return $form @@ -99,7 +102,7 @@ public static function table(Table $table): Table fn ($action): array => [ $action->getModalCancelAction(), ]) - ->fillForm(function (Email $record) { + ->fillForm(function ($record) { $body = $record->html_body; return [ @@ -113,7 +116,7 @@ public static function table(Table $table): Table Action::make('resend') ->label(__('Send again')) ->icon('heroicon-o-envelope') - ->action(function (Email $record) { + ->action(function ($record) { try { Mail::to($record->to) ->cc($record->cc) diff --git a/src/Listeners/FilamentEmailLogger.php b/src/Listeners/FilamentEmailLogger.php index 44a0be3..2ae3717 100644 --- a/src/Listeners/FilamentEmailLogger.php +++ b/src/Listeners/FilamentEmailLogger.php @@ -2,6 +2,7 @@ namespace RickDBCN\FilamentEmail\Listeners; +use Illuminate\Support\Facades\Config; use RickDBCN\FilamentEmail\Models\Email; class FilamentEmailLogger @@ -24,7 +25,9 @@ public function handle(object $event): void $rawMessage = $event->sent->getSymfonySentMessage(); $email = $event->message; - Email::create([ + $model = Config::get('filament-email.resource.model') ?? Email::class; + + $model::create([ 'from' => $this->recipientsToString($email->getFrom()), 'to' => $this->recipientsToString($email->getTo()), 'cc' => $this->recipientsToString($email->getCc()), diff --git a/src/Mail/ResendMail.php b/src/Mail/ResendMail.php index 63b532e..7ea66f2 100644 --- a/src/Mail/ResendMail.php +++ b/src/Mail/ResendMail.php @@ -7,15 +7,14 @@ use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; -use RickDBCN\FilamentEmail\Models\Email; class ResendMail extends Mailable { use Queueable, SerializesModels; - public Email $email; + public $email; - public function __construct(Email $email) + public function __construct($email) { $this->email = $email; } diff --git a/tests/EmailModelTest.php b/tests/EmailModelTest.php index 2965fe5..cfa666b 100644 --- a/tests/EmailModelTest.php +++ b/tests/EmailModelTest.php @@ -1,6 +1,7 @@ model = Config::get('filament-email.resource.model') ?? Email::class; +}); + it('can create an Email model', function () { $emailModel = Email::factory()->create(); assertModelExists($emailModel); @@ -26,17 +31,17 @@ assertDatabaseCount('filament_email_log', 1); - assertEquals(Email::first()->to, $recipient); + assertEquals($this->model::first()->to, $recipient); }); it('can render table page', function () { - Email::factory()->create(); + $this->model::factory()->create(); livewire(ListEmails::class)->assertSuccessful(); }); it('can resend email', function () { - $email = Email::factory()->create(); + $email = $this->model::factory()->create(); livewire(ListEmails::class) ->callTableAction('resend', $email); - assertDatabaseCount((new Email)->getTable(), 2); + assertDatabaseCount((new $this->model)->getTable(), 2); });