Skip to content

Commit 1e0a005

Browse files
author
=
committed
refactor: update property type for events in Webhook model and enhance server status checks in event listener
1 parent 7c93432 commit 1e0a005

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

discord-webhooks/src/Models/Webhook.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @property string $name
1616
* @property string $webhook_url
1717
* @property int|null $server_id
18-
* @property array<WebhookEvent> $events
18+
* @property array<string> $events
1919
* @property bool $enabled
2020
* @property Carbon|null $last_triggered_at
2121
* @property Carbon $created_at
@@ -60,7 +60,8 @@ public function server(): BelongsTo
6060
*/
6161
public function hasEvent(WebhookEvent|string $event): bool
6262
{
63-
$value = $event instanceof WebhookEvent ? $event->value : $event;
63+
$value = $event instanceof WebhookEvent ? $event->value : (string)$event;
64+
// $this->events ist array<string>
6465
return in_array($value, $this->events, true);
6566
}
6667

discord-webhooks/src/Providers/WebhooksPluginProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Notjami\Webhooks\Providers;
44

5+
use Illuminate\Support\Facades\DB;
56
use App\Models\Role;
67
use App\Models\Server;
78
use Illuminate\Console\Scheduling\Schedule;
@@ -42,14 +43,20 @@ protected function registerServerStatusListeners(): void
4243
// Server Installation Events
4344
Event::listen('eloquent.updating: App\Models\Server', function (Server $server) {
4445
// Check if status changed to installing
45-
if ($server->isDirty('status') && $server->status === 'installing') {
46+
if (
47+
$server->isDirty('status') &&
48+
((is_string($server->status) && $server->status === 'installing') ||
49+
(method_exists($server->status, 'value') && $server->status->value === 'installing'))
50+
) {
4651
DB::afterCommit(function () use ($server) {
4752
app(DiscordWebhookService::class)->triggerEvent(WebhookEvent::ServerInstalling, $server);
4853
});
4954
}
5055

5156
// Check if installation completed (status changed from installing to null/running)
52-
if ($server->isDirty('status') && $server->getOriginal('status') === 'installing' && $server->status === null) {
57+
$original = $server->getOriginal('status');
58+
$isOriginalInstalling = (is_string($original) && $original === 'installing') || (method_exists($original, 'value') && $original->value === 'installing');
59+
if ($server->isDirty('status') && $isOriginalInstalling && $server->status === null) {
5360
DB::afterCommit(function () use ($server) {
5461
app(DiscordWebhookService::class)->triggerEvent(WebhookEvent::ServerInstalled, $server);
5562
});

discord-webhooks/src/Services/DiscordWebhookService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public function triggerEvent(WebhookEvent $event, Server $server): void
105105
}
106106
}
107107

108+
/**
109+
* @param array<string, mixed> $payload
110+
*/
108111
protected function send(Webhook $webhook, array $payload): bool
109112
{
110113
// Enforce Discord webhook URL pattern as a second layer of validation

0 commit comments

Comments
 (0)