*/ class TenantConfigurationResourceFactory extends Factory { protected $model = TenantConfigurationResource::class; public function definition(): array { return [ 'managed_environment_id' => ManagedEnvironment::factory()->for(Workspace::factory()), 'workspace_id' => function (array $attributes): int { return $this->workspaceIdForTenant((int) ($attributes['managed_environment_id'] ?? 0)); }, 'provider_connection_id' => function (array $attributes): int { $tenantId = (int) ($attributes['managed_environment_id'] ?? 0); $workspaceId = $this->workspaceIdForTenant($tenantId); return (int) ProviderConnection::factory()->create([ 'workspace_id' => $workspaceId, 'managed_environment_id' => $tenantId, ])->getKey(); }, 'resource_type_id' => TenantConfigurationResourceType::factory(), 'latest_evidence_id' => null, 'source_class' => SourceClass::Tcm->value, 'canonical_type' => function (array $attributes): string { $resourceType = TenantConfigurationResourceType::query()->find((int) ($attributes['resource_type_id'] ?? 0)); return $resourceType instanceof TenantConfigurationResourceType ? (string) $resourceType->canonical_type : fake()->slug(); }, 'canonical_resource_key' => fn (array $attributes): string => sprintf( '%s:%s', (string) ($attributes['canonical_type'] ?? 'resource'), fake()->uuid(), ), 'source_resource_id' => fake()->uuid(), 'source_display_name' => fake()->words(3, true), 'source_metadata' => ['factory' => 'tenant_configuration_resource'], 'latest_evidence_state' => EvidenceState::NotCaptured->value, 'latest_identity_state' => IdentityState::Stable->value, 'latest_claim_state' => ClaimState::InternalOnly->value, 'latest_payload_hash' => null, 'latest_captured_at' => null, ]; } private function workspaceIdForTenant(int $tenantId): int { $tenant = ManagedEnvironment::query()->find($tenantId); if (! $tenant instanceof ManagedEnvironment) { return (int) Workspace::factory()->create()->getKey(); } if ($tenant->workspace_id === null) { $workspaceId = (int) Workspace::factory()->create()->getKey(); $tenant->forceFill(['workspace_id' => $workspaceId])->save(); return $workspaceId; } return (int) $tenant->workspace_id; } }