lasthourcloud/app/Notifications/Application/StatusChanged.php

77 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2023-07-28 08:55:26 +00:00
namespace App\Notifications\Application;
use App\Notifications\Channels\DiscordChannel;
use App\Notifications\Channels\EmailChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
2023-07-28 08:55:26 +00:00
class StatusChanged extends Notification implements ShouldQueue
{
use Queueable;
2023-08-07 20:14:21 +00:00
public $application;
public string $application_name;
public string|null $application_url = null;
public string $project_uuid;
public string $environment_name;
2023-08-07 20:14:21 +00:00
public string|null $fqdn;
2023-08-07 20:14:21 +00:00
public function __construct($application)
{
$this->application = $application;
$this->application_name = data_get($application, 'name');
$this->project_uuid = data_get($application, 'environment.project.uuid');
$this->environment_name = data_get($application, 'environment.name');
2023-08-07 20:14:21 +00:00
$this->fqdn = data_get($application, 'fqdn', null);
if (Str::of($this->fqdn)->explode(',')->count() > 1) {
$this->fqdn = Str::of($this->fqdn)->explode(',')->first();
}
$this->application_url = base_url() . "/project/{$this->project_uuid}/{$this->environment_name}/application/{$this->application->uuid}";
}
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_status_changes');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_status_changes');
2023-07-26 13:20:04 +00:00
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
$channels[] = EmailChannel::class;
}
2023-07-26 13:20:04 +00:00
if ($isDiscordEnabled && $isSubscribedToDiscordEvent) {
$channels[] = DiscordChannel::class;
}
return $channels;
}
public function toMail(): MailMessage
{
2023-07-26 13:20:04 +00:00
$mail = new MailMessage();
$fqdn = $this->fqdn;
$mail->subject("{$this->application_name} has been stopped");
2023-07-28 08:55:26 +00:00
$mail->view('emails.application-status-changes', [
2023-07-26 13:20:04 +00:00
'name' => $this->application_name,
'fqdn' => $fqdn,
'application_url' => $this->application_url,
]);
return $mail;
}
public function toDiscord(): string
{
$message = '⛔ ' . $this->application_name . ' has been stopped.
';
$message .= '[Application URL](' . $this->application_url . ')';
return $message;
}
}