45 lines
1 KiB
PHP
45 lines
1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Set\CreateSetDto;
|
|
use App\Set\SetRepository;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\UserRepository;
|
|
use Illuminate\Database\Seeder;
|
|
use RuntimeException;
|
|
|
|
class SetSeeder extends Seeder
|
|
{
|
|
private const array NAMES = [
|
|
'Bible',
|
|
'Course',
|
|
'Fitness Program',
|
|
];
|
|
|
|
public function run(): void
|
|
{
|
|
$user = app(UserRepository::class)->findByEmail(
|
|
new EmailAddress(UserSeeder::EMAIL),
|
|
);
|
|
if ($user === null) {
|
|
throw new RuntimeException('seeded user not found');
|
|
}
|
|
|
|
$repository = app(SetRepository::class);
|
|
$existingNames = array_map(function ($set): string {
|
|
return $set->getName();
|
|
}, $repository->all());
|
|
|
|
foreach (self::NAMES as $name) {
|
|
if (in_array($name, $existingNames, true)) {
|
|
continue;
|
|
}
|
|
|
|
$repository->create(new CreateSetDto(
|
|
name: $name,
|
|
creator: $user,
|
|
));
|
|
}
|
|
}
|
|
}
|