80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Domain\Identity\AccountStatus;
|
|
use App\Domain\Identity\Party;
|
|
use App\Domain\Identity\PartyType;
|
|
use App\Domain\Identity\Profile;
|
|
use App\Domain\Identity\StaffRole;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @extends Factory<User>
|
|
*/
|
|
class UserFactory extends Factory
|
|
{
|
|
protected static ?string $password;
|
|
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'name' => fake()->name(),
|
|
'email' => fake()->unique()->safeEmail(),
|
|
'email_verified_at' => now(),
|
|
'password' => static::$password ??= Hash::make('password'),
|
|
'status' => AccountStatus::Aktiv,
|
|
'confirmed_adult_at' => now(),
|
|
'remember_token' => Str::random(10),
|
|
];
|
|
}
|
|
|
|
public function configure(): static
|
|
{
|
|
return $this->afterMaking(function (User $user): void {
|
|
if ($user->party_id !== null) {
|
|
return;
|
|
}
|
|
|
|
$party = Party::query()->create([
|
|
'type' => PartyType::Person,
|
|
'display_name' => $user->name,
|
|
]);
|
|
|
|
$user->party_id = $party->id;
|
|
})->afterCreating(function (User $user): void {
|
|
Profile::query()->firstOrCreate(
|
|
['party_id' => $user->party_id],
|
|
[
|
|
'display_name' => $user->name,
|
|
'visibility' => ['display_name' => 'private'],
|
|
],
|
|
);
|
|
});
|
|
}
|
|
|
|
public function unverified(): static
|
|
{
|
|
return $this->state(fn (array $attributes) => [
|
|
'email_verified_at' => null,
|
|
]);
|
|
}
|
|
|
|
public function redakteur(): static
|
|
{
|
|
return $this->afterCreating(function (User $user): void {
|
|
$user->staffRoles()->create(['role' => StaffRole::Redakteur]);
|
|
});
|
|
}
|
|
|
|
public function systemverwalter(): static
|
|
{
|
|
return $this->afterCreating(function (User $user): void {
|
|
$user->staffRoles()->create(['role' => StaffRole::Systemverwalter]);
|
|
});
|
|
}
|
|
}
|