TenantAtlas/database/factories/TenantOnboardingSessionFactory.php

159 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Tenant;
use App\Models\TenantOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Onboarding\OnboardingCheckpoint;
use App\Support\Onboarding\OnboardingLifecycleState;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<TenantOnboardingSession>
*/
class TenantOnboardingSessionFactory extends Factory
{
protected $model = TenantOnboardingSession::class;
public function definition(): array
{
$entraTenantId = fake()->uuid();
$tenantName = fake()->company();
return [
'workspace_id' => Workspace::factory(),
'tenant_id' => null,
'entra_tenant_id' => $entraTenantId,
'current_step' => 'identify',
'state' => [
'entra_tenant_id' => $entraTenantId,
'tenant_name' => $tenantName,
'environment' => 'prod',
],
'started_by_user_id' => User::factory(),
'updated_by_user_id' => User::factory(),
'version' => 1,
'lifecycle_state' => OnboardingLifecycleState::Draft->value,
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
'last_completed_checkpoint' => null,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => null,
'cancelled_at' => null,
];
}
public function forWorkspace(Workspace $workspace): static
{
return $this->state(fn (): array => [
'workspace_id' => (int) $workspace->getKey(),
]);
}
public function forTenant(Tenant $tenant): static
{
return $this->state(fn (array $attributes): array => [
'workspace_id' => (int) $tenant->workspace_id,
'tenant_id' => (int) $tenant->getKey(),
'entra_tenant_id' => (string) $tenant->tenant_id,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'tenant_id' => (int) $tenant->getKey(),
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'environment' => (string) ($tenant->environment ?? 'prod'),
]),
]);
}
public function startedBy(User $user): static
{
return $this->state(fn (): array => [
'started_by_user_id' => (int) $user->getKey(),
]);
}
public function updatedBy(User $user): static
{
return $this->state(fn (): array => [
'updated_by_user_id' => (int) $user->getKey(),
]);
}
public function withProviderConnection(int $providerConnectionId): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'connection',
'current_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
'last_completed_checkpoint' => OnboardingCheckpoint::Identify->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'provider_connection_id' => $providerConnectionId,
]),
]);
}
public function withVerificationRun(int $operationRunId): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'verify',
'current_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
'last_completed_checkpoint' => OnboardingCheckpoint::ConnectProvider->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'verification_operation_run_id' => $operationRunId,
]),
]);
}
public function reviewReady(): static
{
return $this->state(fn (array $attributes): array => [
'current_step' => 'bootstrap',
'lifecycle_state' => OnboardingLifecycleState::ReadyForActivation->value,
'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'last_completed_checkpoint' => OnboardingCheckpoint::VerifyAccess->value,
'state' => array_merge(is_array($attributes['state'] ?? null) ? $attributes['state'] : [], [
'bootstrap_operation_types' => [],
]),
]);
}
public function completed(): static
{
return $this->state(fn (): array => [
'current_step' => 'complete',
'lifecycle_state' => OnboardingLifecycleState::Completed->value,
'current_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'last_completed_checkpoint' => OnboardingCheckpoint::CompleteActivate->value,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => now(),
'cancelled_at' => null,
]);
}
public function cancelled(): static
{
return $this->state(fn (): array => [
'current_step' => 'cancelled',
'lifecycle_state' => OnboardingLifecycleState::Cancelled->value,
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
'last_completed_checkpoint' => null,
'reason_code' => null,
'blocking_reason_code' => null,
'completed_at' => null,
'cancelled_at' => now(),
]);
}
public function resumableForTenant(Tenant $tenant): static
{
return $this->forTenant($tenant)->state(fn (): array => [
'completed_at' => null,
'cancelled_at' => null,
]);
}
}