59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Models\WebhookEndpoint;
|
|
use Illuminate\Broadcasting\Channel;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Broadcasting\PrivateChannel;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
|
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
|
|
class WebhookRequestReceived implements ShouldBroadcast, ShouldDispatchAfterCommit
|
|
{
|
|
use Dispatchable, InteractsWithSockets;
|
|
|
|
private ?bool $isPublic;
|
|
|
|
public function __construct(
|
|
public readonly string $endpointId,
|
|
public readonly string $requestId,
|
|
?bool $isPublic = null,
|
|
) {
|
|
$this->isPublic = $isPublic;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, Channel>
|
|
*/
|
|
public function broadcastOn(): array
|
|
{
|
|
$isPublic = $this->isPublic ?? WebhookEndpoint::query()
|
|
->whereKey($this->endpointId)
|
|
->value('is_public');
|
|
|
|
return [
|
|
$isPublic
|
|
? new Channel('webhooks.'.$this->endpointId)
|
|
: new PrivateChannel('webhooks.'.$this->endpointId),
|
|
];
|
|
}
|
|
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'webhook.request.received';
|
|
}
|
|
|
|
/**
|
|
* @return array{endpointId: string, requestId: string}
|
|
*/
|
|
public function broadcastWith(): array
|
|
{
|
|
return [
|
|
'endpointId' => $this->endpointId,
|
|
'requestId' => $this->requestId,
|
|
];
|
|
}
|
|
}
|