Files
mytodos/database/factories/OrganizationFactory.php
T
2026-11-04 11:10:05 -08:00

89 lines
2.3 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Organization;
use App\Models\Plan;
use App\Models\User;
use App\OrganizationRole;
use App\WorkspaceType;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Organization>
*/
class OrganizationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->company(),
'type' => WorkspaceType::Organization,
'personal_workspace' => false,
'lifetime_access' => false,
];
}
/**
* Indicate that the organization is a single user's personal workspace.
*/
public function personal(): static
{
return $this->state(fn (array $attributes) => [
'type' => WorkspaceType::Personal,
'personal_workspace' => true,
]);
}
/**
* Indicate that the organization is a shared family workspace.
*/
public function family(): static
{
return $this->state(fn (array $attributes) => [
'type' => WorkspaceType::Family,
'personal_workspace' => false,
]);
}
/**
* Give the organization perpetual access, as granted to accounts that
* predate subscriptions.
*/
public function withLifetimeAccess(): static
{
return $this->state(fn (array $attributes) => [
'lifetime_access' => true,
]);
}
/**
* Give the organization the plan with the given key.
*/
public function withPlan(string $key): static
{
return $this->afterCreating(function (Organization $organization) use ($key): void {
$planId = Plan::query()->where('key', $key)->value('id');
if ($planId !== null) {
$organization->forceFill(['plan_id' => $planId])->save();
}
});
}
/**
* Add the given user to the organization with the given role.
*/
public function withMember(User $user, OrganizationRole $role = OrganizationRole::Owner): static
{
return $this->afterCreating(function (Organization $organization) use ($user, $role): void {
$organization->users()->attach($user, ['role' => $role->value]);
});
}
}