TIDE/backend/database/seeders/CommentSeeder.php
Yisroel Baum beb4ddd1e9
split seeders by domain entity
Replaces the laravel-default scaffold seeder with one file per
domain (AdminUser, User, Post, Comment) plus a config/seed.php
to source admin credentials from env. DatabaseSeeder is now a
pure orchestrator that calls the sub-seeders in dependency
order.
2026-05-06 22:38:14 +03:00

53 lines
1.5 KiB
PHP

<?php
namespace Database\Seeders;
use App\Comment\CommentRepository;
use App\Comment\CreateCommentDto;
use App\Post\PostRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\UserRepository;
use DateTimeImmutable;
use DateTimeZone;
use Illuminate\Database\Seeder;
class CommentSeeder extends Seeder
{
public function __construct(
private CommentRepository $commentRepo,
private PostRepository $postRepo,
private UserRepository $userRepo,
) {}
public function run(): void
{
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$bob = $this->userRepo->findByEmail(
new EmailAddress('bob@example.com'),
);
$carol = $this->userRepo->findByEmail(
new EmailAddress('carol@example.com'),
);
if ($bob === null || $carol === null) {
return;
}
$posts = $this->postRepo->findRecent(10);
if (count($posts) === 0) {
return;
}
$firstPost = $posts[0];
$this->commentRepo->create(new CreateCommentDto(
postId: $firstPost->getId(),
userId: $bob->getId(),
body: 'Welcome aboard!',
createdAt: $now,
));
$this->commentRepo->create(new CreateCommentDto(
postId: $firstPost->getId(),
userId: $carol->getId(),
body: 'Looking forward to more.',
createdAt: $now->modify('+1 minute'),
));
}
}