byos_laravel/app/Notifications/BatteryLow.php
Benjamin Nussbaum 91e222f7a6
Some checks failed
tests / ci (push) Has been cancelled
chore: rector
2025-10-02 22:29:46 +02:00

66 lines
1.9 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\Device;
use App\Notifications\Channels\WebhookChannel;
use App\Notifications\Messages\WebhookMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class BatteryLow extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*/
public function __construct(private Device $device) {}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail', WebhookChannel::class];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->markdown('mail.battery-low', ['device' => $this->device]);
}
public function toWebhook(object $notifiable): \App\Notifications\Messages\WebhookMessage
{
return WebhookMessage::create()
->data([
'topic' => config('services.webhook.notifications.topic', 'battery.low'),
'message' => "Battery below {$this->device->battery_percent}% on device: {$this->device->name}",
'device_id' => $this->device->id,
'device_name' => $this->device->name,
'battery_percent' => $this->device->battery_percent,
])
->userAgent(config('app.name'))
->header('X-TrmnlByos-Event', 'battery.low');
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'device_name' => $this->device->name,
'battery_percent' => $this->device->battery_percent,
];
}
}