TenantAtlas/app/Services/Alerts/TeamsWebhookSender.php
ahmido 3ed275cef3 feat(alerts): Monitoring cluster + v1 resources (spec 099) (#121)
Implements spec `099-alerts-v1-teams-email`.

- Monitoring navigation: Alerts as a cluster under Monitoring; default landing is Alert deliveries.
- Tenant panel: Alerts points to `/admin/alerts` and the cluster navigation is hidden in tenant panel.
- Guard compliance: removes direct `Gate::` usage from Alert resources so `NoAdHocFilamentAuthPatternsTest` passes.

Verification:
- Full suite: `1348 passed, 7 skipped` (EXIT=0).

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #121
2026-02-18 15:20:43 +00:00

59 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Alerts;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class TeamsWebhookSender
{
/**
* @param array<string, mixed> $payload
*/
public function send(string $webhookUrl, array $payload): void
{
$webhookUrl = trim($webhookUrl);
if ($webhookUrl === '') {
throw new RuntimeException('Teams webhook URL is not configured.');
}
$response = Http::timeout((int) config('tenantpilot.alerts.http_timeout_seconds', 10))
->asJson()
->post($webhookUrl, [
'text' => $this->toTeamsTextPayload($payload),
]);
if ($response->successful()) {
return;
}
throw new RuntimeException(sprintf(
'Teams delivery failed with HTTP status %d.',
(int) $response->status(),
));
}
/**
* @param array<string, mixed> $payload
*/
private function toTeamsTextPayload(array $payload): string
{
$title = trim((string) Arr::get($payload, 'title', 'Alert'));
$body = trim((string) Arr::get($payload, 'body', 'A matching alert event was detected.'));
if ($title === '') {
$title = 'Alert';
}
if ($body === '') {
return $title;
}
return $title."\n\n".$body;
}
}