71 lines
1.6 KiB
PHP
71 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Enums\CrawlOutcomeEnum;
|
|
use App\Models\Page;
|
|
use App\Models\PageCrawl;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
/**
|
|
* @extends Factory<PageCrawl>
|
|
*/
|
|
class PageCrawlFactory extends Factory
|
|
{
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'page_id' => null,
|
|
'domain' => 'example.com',
|
|
'priority' => 0,
|
|
'scheduled_for' => now(),
|
|
'completed_at' => null,
|
|
'outcome' => null,
|
|
'status_code' => null,
|
|
'error_message' => null,
|
|
'locked_at' => null,
|
|
];
|
|
}
|
|
|
|
public function page(Page $page): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'page_id' => $page->id,
|
|
]);
|
|
}
|
|
|
|
public function successful(): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'outcome' => CrawlOutcomeEnum::Success,
|
|
'completed_at' => now(),
|
|
]);
|
|
}
|
|
|
|
public function failed(string $errorMessage): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'outcome' => CrawlOutcomeEnum::Failed,
|
|
'completed_at' => now(),
|
|
'error_message' => $errorMessage,
|
|
]);
|
|
}
|
|
|
|
public function scheduledAt(Carbon $scheduledAt): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'scheduled_for' => $scheduledAt,
|
|
]);
|
|
}
|
|
|
|
public function locked(): static
|
|
{
|
|
return $this->state(fn () => [
|
|
'locked_at' => now(),
|
|
'outcome' => null,
|
|
]);
|
|
}
|
|
}
|