Files
Gnuboard7/modules/_bundled/sirsoft-board/database/seeders/Sample/BoardMailSendLogSeeder.php
T
2026-04-01 10:30:52 +09:00

142 lines
4.6 KiB
PHP

<?php
namespace Modules\Sirsoft\Board\Database\Seeders\Sample;
use App\Enums\ExtensionOwnerType;
use App\Enums\MailSendStatus;
use App\Models\MailSendLog;
use App\Traits\HasSeederCounts;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
/**
* 게시판 모듈 메일 발송 이력 개발용 시더
*
* 게시판 메일 템플릿(new_comment, reply_comment, post_reply, post_action, new_post_admin)을
* 기반으로 발송 이력 샘플 데이터를 생성합니다.
*
* 개발/테스트 용도이므로 설치 시 자동 실행되지 않습니다.
* 수동 실행: php artisan module:seed sirsoft-board --class=BoardMailSendLogSeeder
*/
class BoardMailSendLogSeeder extends Seeder
{
use HasSeederCounts;
/**
* 게시판 메일 템플릿 유형별 제목 패턴
*
* @var array<string, string>
*/
private array $templateSubjects = [
'new_comment' => '[그누보드7] 게시글에 새 댓글이 등록되었습니다',
'reply_comment' => '[그누보드7] 댓글에 답글이 등록되었습니다',
'post_reply' => '[그누보드7] 게시글에 답변글이 등록되었습니다',
'post_action' => '[그누보드7] 게시글 관리자 처리 안내',
'new_post_admin' => '[그누보드7] 새 게시글이 등록되었습니다',
];
/**
* 시더 실행
*
* @return void
*/
public function run(): void
{
$count = $this->getSeederCount('mail_send_logs', 25);
$this->command->info('게시판 메일 발송 이력 시딩 시작...');
$templateTypes = array_keys($this->templateSubjects);
$recipients = $this->getRecipients();
for ($i = 0; $i < $count; $i++) {
$templateType = $templateTypes[array_rand($templateTypes)];
$recipient = $recipients[array_rand($recipients)];
$status = $this->randomStatus();
$sentAt = $this->randomSentAt();
MailSendLog::create([
'recipient_email' => $recipient['email'],
'recipient_name' => $recipient['name'],
'subject' => $this->templateSubjects[$templateType],
'template_type' => $templateType,
'extension_type' => ExtensionOwnerType::Module,
'extension_identifier' => 'sirsoft-board',
'source' => 'notification',
'status' => $status->value,
'error_message' => $status === MailSendStatus::Failed
? $this->randomErrorMessage()
: null,
'sent_at' => $sentAt,
]);
}
$this->command->info("게시판 메일 발송 이력 시딩 완료 ({$count}건)");
}
/**
* 샘플 수신자 목록을 반환합니다.
*
* @return array<int, array{email: string, name: string}>
*/
private function getRecipients(): array
{
return [
['email' => 'hong@example.com', 'name' => '홍길동'],
['email' => 'kim@example.com', 'name' => '김철수'],
['email' => 'lee@example.com', 'name' => '이영희'],
['email' => 'park@example.com', 'name' => '박민수'],
['email' => 'choi@example.com', 'name' => '최지은'],
['email' => 'admin@example.com', 'name' => '관리자'],
['email' => 'john@example.com', 'name' => 'John Doe'],
['email' => 'jane@example.com', 'name' => 'Jane Smith'],
];
}
/**
* 가중치 기반 랜덤 상태를 반환합니다.
*
* @return MailSendStatus
*/
private function randomStatus(): MailSendStatus
{
$rand = mt_rand(1, 100);
if ($rand <= 80) {
return MailSendStatus::Sent;
} elseif ($rand <= 95) {
return MailSendStatus::Failed;
}
return MailSendStatus::Skipped;
}
/**
* 최근 90일 내 랜덤 발송 시각을 반환합니다.
*
* @return Carbon
*/
private function randomSentAt(): Carbon
{
return Carbon::now()->subDays(mt_rand(0, 90))->subHours(mt_rand(0, 23))->subMinutes(mt_rand(0, 59));
}
/**
* 랜덤 에러 메시지를 반환합니다.
*
* @return string
*/
private function randomErrorMessage(): string
{
$messages = [
'Connection timed out after 30 seconds',
'SMTP server returned 550: Mailbox not found',
'DNS resolution failed for mail server',
'Authentication failed: Invalid credentials',
'Message rejected: Recipient address rejected',
];
return $messages[array_rand($messages)];
}
}