generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate unused classes and add new Notifier functionality
- Loading branch information
1 parent
808fbb7
commit 1f39ce1
Showing
8 changed files
with
510 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Kiwilan\Steward\Commands; | ||
|
||
use App\Utils\Notifier; | ||
use Illuminate\Console\Command; | ||
|
||
class NotifierCommand extends Commandable | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'notifier | ||
{message : Message to send.} | ||
{--t|type= : `mail`, `slack` or `discord`.} | ||
{--w|webhook : Webhook URL for Slack or Discord.}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send notifications with mail, Discord or Slack.'; | ||
|
||
public function __construct( | ||
protected ?string $message = null, | ||
protected ?string $type = 'discord', | ||
protected ?string $webhook = null, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
$this->title(); | ||
|
||
$this->message = (string) $this->argument('message'); | ||
$this->type = (string) $this->option('type'); | ||
$this->webhook = (string) $this->option('webhook'); | ||
|
||
$this->info("Sending notification to {$this->type}..."); | ||
|
||
if ($this->type === 'mail') { | ||
Notifier::mail() | ||
->auto() | ||
->message($this->message) | ||
->send(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
if ($this->type === 'discord') { | ||
$this->info("Webhook: {$this->webhook}"); | ||
Notifier::discord($this->webhook) | ||
->message($this->message) | ||
->send(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
if ($this->type === 'slack') { | ||
$this->info("Webhook: {$this->webhook}"); | ||
Notifier::slack($this->webhook) | ||
->message($this->message) | ||
->send(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
$this->error('Type not found.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Utils; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class Journal | ||
{ | ||
protected function __construct( | ||
protected ?string $message = null, | ||
protected string $level = 'info', | ||
protected array $data = [], | ||
protected ?Collection $users = null, | ||
) { | ||
$this->log(); | ||
} | ||
|
||
public static function info(string $message, array $data = []): self | ||
{ | ||
|
||
return new self($message, 'info', $data); | ||
} | ||
|
||
public static function debug(string $message, array $data = []): self | ||
{ | ||
return new self($message, 'debug', $data); | ||
} | ||
|
||
public static function warning(string $message, array $data = []): self | ||
{ | ||
return new self($message, 'warning', $data); | ||
} | ||
|
||
public static function error(string $message, array $data = []): self | ||
{ | ||
return new self($message, 'error', $data); | ||
} | ||
|
||
private function log(): void | ||
{ | ||
Log::log($this->level, $this->message, $this->data); | ||
} | ||
|
||
/** | ||
* Send notification to database for Users with access to Filament admin panel with `filament/notifications` package. | ||
* | ||
* @param Model|Authenticatable|Collection|array|null $users To send notification to. | ||
*/ | ||
public function toDatabase(Model|Authenticatable|Collection|array|null $users): void | ||
{ | ||
if (! class_exists('\Filament\Notifications\Notification')) { | ||
throw new \Exception('Filament notifications is not installed, check https://filamentphp.com/docs/3.x/notifications/installation'); | ||
} | ||
|
||
if (! class_exists('\App\Models\User')) { | ||
throw new \Exception('User model not found'); | ||
} | ||
|
||
$filamentUsers = $this->users; | ||
|
||
if (! $filamentUsers) { | ||
$users = '\App\Models\User'; | ||
$filamentUsers = $users::all()->map(fn ($user) => $user->canAccessPanel()); | ||
} | ||
|
||
\Filament\Notifications\Notification::make() | ||
->title($this->level) | ||
->body($this->message) | ||
->sendToDatabase($filamentUsers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.