Skip to content

Commit

Permalink
Use Model from Config for Resource (#7)
Browse files Browse the repository at this point in the history
* Use Model from Config for Resource

* Update ListMails.php to use config Model

* Update MailStatsWidget.php to use config Model
  • Loading branch information
iAmKevinMcKee authored Dec 27, 2024
1 parent f025911 commit 63233c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/Resources/MailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

class MailResource extends Resource
{
protected static ?string $model = Mail::class;

protected static ?string $slug = 'mails';

protected static ?string $recordTitleAttribute = 'subject';
Expand All @@ -39,6 +37,11 @@ class MailResource extends Resource

protected static bool $shouldRegisterNavigation = true;

public static function getModel(): string
{
return config('mails.models.mail');
}

public static function getNavigationGroup(): ?string
{
return __('Mails');
Expand Down
16 changes: 9 additions & 7 deletions src/Resources/MailResource/Pages/ListMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,48 @@ protected function getActions(): array

public function getTabs(): array
{
$class = config('mails.models.mail');

return [
'all' => Tab::make()
->label(__('All'))
->badgeColor('primary')
->icon('heroicon-o-inbox')
->badge(Mail::count()),
->badge($class::count()),

'sent' => Tab::make()
->label(__('Sent'))
->badgeColor('info')
->icon('heroicon-o-paper-airplane')
->badge(Mail::sent()->count())
->badge($class::sent()->count())
->modifyQueryUsing(fn (Builder $query) => $query->sent()),

Check failure on line 42 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::sent().

Check failure on line 42 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::sent().

'delivered' => Tab::make()
->label(__('Delivered'))
->badgeColor('success')
->icon('heroicon-o-check-circle')
->badge(Mail::delivered()->count())
->badge($class::delivered()->count())
->modifyQueryUsing(fn (Builder $query) => $query->delivered()),

Check failure on line 49 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::delivered().

Check failure on line 49 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::delivered().

'opened' => Tab::make()
->label(__('Opened'))
->badgeColor('info')
->icon('heroicon-o-eye')
->badge(Mail::opened()->count())
->badge($class::opened()->count())
->modifyQueryUsing(fn (Builder $query) => $query->opened()),

Check failure on line 56 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::opened().

Check failure on line 56 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::opened().

'clicked' => Tab::make()
->label(__('Clicked'))
->badgeColor('clicked')
->icon('heroicon-o-cursor-arrow-rays')
->badge(Mail::clicked()->count())
->badge($class::clicked()->count())
->modifyQueryUsing(fn (Builder $query) => $query->clicked()),

Check failure on line 63 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::clicked().

Check failure on line 63 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::clicked().

'bounced' => Tab::make()
->label(__('Bounced'))
->badgeColor('danger')
->icon('heroicon-o-x-circle')
->badge(fn () => Mail::softBounced()->count() + Mail::hardBounced()->count())
->badge(fn () => $class::softBounced()->count() + $class::hardBounced()->count())
->modifyQueryUsing(fn (Builder $query) => $query->where(function ($query) {
$query->softBounced()->orWhere(function ($query) {

Check failure on line 71 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::softBounced().

Check failure on line 71 in src/Resources/MailResource/Pages/ListMails.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::softBounced().
$query->hardBounced();
Expand All @@ -75,7 +77,7 @@ public function getTabs(): array
->label(__('Unsent'))
->badgeColor('gray')
->icon('heroicon-o-x-circle')
->badge(Mail::unsent()->count())
->badge($class::unsent()->count())
->modifyQueryUsing(fn (Builder $query) => $query->unsent()),
];
}
Expand Down
14 changes: 8 additions & 6 deletions src/Resources/MailResource/Widgets/MailStatsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class MailStatsWidget extends BaseWidget

protected function getStats(): array
{
$bouncedMails = Mail::where(fn ($query) => $query->softBounced()->orWhere(fn ($query) => $query->hardBounced()))->count();
$openedMails = Mail::opened()->count();
$deliveredMails = Mail::delivered()->count();
$clickedMails = Mail::clicked()->count();

$mailCount = Mail::count();
$class = config('mails.models.mail');

$bouncedMails = $class::where(fn ($query) => $query->softBounced()->orWhere(fn ($query) => $query->hardBounced()))->count();
$openedMails = $class::opened()->count();
$deliveredMails = $class::delivered()->count();
$clickedMails = $class::clicked()->count();

$mailCount = $class::count();

if ($mailCount === 0) {
return [];
Expand Down

0 comments on commit 63233c8

Please sign in to comment.