Skip to content

Commit

Permalink
Replaced Config::get with config helper method. Customizabile prune c…
Browse files Browse the repository at this point in the history
…rontab schedule from config file.
  • Loading branch information
Marco Germani committed Apr 26, 2024
1 parent 47e3ba0 commit 905708f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 2 additions & 0 deletions config/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
'keep_email_for_days' => 60,
'label' => null,

'prune_crontab' => '0 0 * * *',

'can_access' => [
'role' => [],
],
Expand Down
11 changes: 5 additions & 6 deletions src/Filament/Resources/EmailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
Expand All @@ -44,22 +43,22 @@ public static function getBreadcrumb(): string

public static function getNavigationLabel(): string
{
return Config::get('filament-email.label') ?? __('filament-email::filament-email.navigation_label');
return config('filament-email.label') ?? __('filament-email::filament-email.navigation_label');
}

public static function getNavigationGroup(): ?string
{
return Config::get('filament-email.resource.group') ?? __('filament-email::filament-email.navigation_group');
return config('filament-email.resource.group') ?? __('filament-email::filament-email.navigation_group');
}

public static function getNavigationSort(): ?int
{
return Config::get('filament-email.resource.sort') ?? parent::getNavigationSort();
return config('filament-email.resource.sort') ?? parent::getNavigationSort();
}

public static function getModel(): string
{
return Config::get('filament-email.resource.model') ?? Email::class;
return config('filament-email.resource.model', Email::class);
}

public static function form(Form $form): Form
Expand Down Expand Up @@ -351,7 +350,7 @@ public static function getPages(): array

public static function canAccess(): bool
{
$roles = config('filament-email.can_access.role') ?? [];
$roles = config('filament-email.can_access.role', []);

if (method_exists(auth()->user(), 'hasRole') && ! empty($roles)) {
return auth()->user()->hasRole($roles);
Expand Down
7 changes: 4 additions & 3 deletions src/FilamentEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace RickDBCN\FilamentEmail;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Config;
use RickDBCN\FilamentEmail\Models\Email;
use RickDBCN\FilamentEmail\Providers\EmailMessageServiceProvider;
use Spatie\LaravelPackageTools\Package;
Expand Down Expand Up @@ -36,10 +35,12 @@ public function configurePackage(Package $package): void
public function bootingPackage()
{
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$modelClass = Config::get('filament-email.resource.model') ?? Email::class;
$runCrontab = config('filament-email.prune_crontab', '0 0 * * *');
$modelClass = config('filament-email.resource.model', Email::class);
$class = get_class(new $modelClass);
if (class_exists($class)) {
$schedule->command('model:prune --model="'.$class.'"')->daily();
$schedule->command('model:prune --model="' . $class . '"')
->cron($runCrontab);
}
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/Listeners/FilamentEmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace RickDBCN\FilamentEmail\Listeners;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RickDBCN\FilamentEmail\Models\Email;
Expand All @@ -27,7 +26,7 @@ public function handle(object $event): void
$rawMessage = $event->sent->getSymfonySentMessage();
$email = $event->message;

$model = Config::get('filament-email.resource.model') ?? Email::class;
$model = config('filament-email.resource.model', Email::class);

$attachments = [];
$savePath = 'filament-email-log'.DIRECTORY_SEPARATOR.date('YmdHis').'_'.Str::random(5).DIRECTORY_SEPARATOR;
Expand Down
5 changes: 2 additions & 3 deletions src/Models/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;

/**
* Email
Expand Down Expand Up @@ -78,7 +77,7 @@ public static function boot()

public function prunable()
{
return static::where('created_at', '<=', now()->subDays(Config::get('filament-email.keep_email_for_days')));
return static::where('created_at', '<=', now()->subDays(config('filament-email.keep_email_for_days', 60)));
}

private function getTableColumns()
Expand All @@ -89,7 +88,7 @@ private function getTableColumns()
private function getSearchableFields()
{
$columns = $this->getTableColumns();
$fields = Config::get('filament-email.resource.table_search_fields', $this->defaultSearchFields);
$fields = config('filament-email.resource.table_search_fields', $this->defaultSearchFields);

return Arr::where($fields, function ($value) use ($columns) {
return in_array($value, $columns);
Expand Down

0 comments on commit 905708f

Please sign in to comment.