Files
KanbanGit/database/factories/NotificationDeliveryLogFactory.php

99 lines
3.1 KiB
PHP

<?php
namespace Database\Factories;
use App\Enums\NotificationChannel;
use App\Enums\NotificationDeliveryStatus;
use App\Enums\NotificationEventType;
use App\Models\NotificationAction;
use App\Models\NotificationDeliveryLog;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
/**
* @extends Factory<NotificationDeliveryLog>
*/
class NotificationDeliveryLogFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'attempt_count' => 0,
'channel' => NotificationChannel::Webhook,
'delivered_at' => null,
'delivery_status' => NotificationDeliveryStatus::Pending,
'error_excerpt' => null,
'event_type' => NotificationEventType::TicketStatusChanged,
'http_status' => null,
'last_attempted_at' => null,
'notification_action_id' => NotificationAction::factory()->webhook(),
'payload_metadata' => [
'project_key' => fake()->regexify('[A-Z]{4}'),
'ticket_key' => fake()->regexify('[A-Z]{4}-[0-9]{1,3}'),
],
'response_excerpt' => null,
];
}
public function forNotificationAction(NotificationAction $notificationAction): static
{
return $this->for($notificationAction, 'notificationAction')->state(fn (): array => [
'channel' => $notificationAction->channel,
'event_type' => $notificationAction->event_type,
]);
}
public function failed(): static
{
return $this->state(fn (): array => [
'attempt_count' => 3,
'delivery_status' => NotificationDeliveryStatus::Failed,
'error_excerpt' => 'Webhook returned a permanent validation error.',
'http_status' => 422,
'last_attempted_at' => Carbon::now(),
]);
}
public function pending(): static
{
return $this->state(fn (): array => [
'attempt_count' => 0,
'delivered_at' => null,
'delivery_status' => NotificationDeliveryStatus::Pending,
'error_excerpt' => null,
'http_status' => null,
'last_attempted_at' => null,
'response_excerpt' => null,
]);
}
public function retryable(): static
{
return $this->state(fn (): array => [
'attempt_count' => 1,
'delivery_status' => NotificationDeliveryStatus::Retryable,
'error_excerpt' => 'Connection timed out while contacting the remote endpoint.',
'http_status' => null,
'last_attempted_at' => Carbon::now(),
]);
}
public function succeeded(): static
{
return $this->state(fn (): array => [
'attempt_count' => 1,
'delivered_at' => Carbon::now(),
'delivery_status' => NotificationDeliveryStatus::Succeeded,
'error_excerpt' => null,
'http_status' => 204,
'last_attempted_at' => Carbon::now(),
'response_excerpt' => 'No Content',
]);
}
}