File manager - Edit - /var/www/html/homologBancodetalentos/app/Http/Services/TalentBank/EmailService.php
Back
<?php namespace App\Http\Services\TalentBank; use App\Jobs\TalentBank\SendTalentBankEmailJob; use App\Models\TalentBank\EmailSendLog; use Illuminate\Mail\Mailable; class EmailService { /** * Enfileira e-mail com deduplicação e log (nunca síncrono na request). * * @param array<string, mixed> $meta */ public function queue( string $notificationType, string $recipientType, ?int $recipientId, string $email, Mailable $mailable, string $dedupeKey, ?int $jobListingId = null, ?int $candidateId = null, array $meta = [], ): void { if ($email === '' || ! filter_var($email, FILTER_VALIDATE_EMAIL)) { return; } if ($this->wasRecentlySent($dedupeKey)) { $this->logSkipped($notificationType, $recipientType, $recipientId, $email, $mailable, $dedupeKey, $jobListingId, $candidateId); return; } $log = EmailSendLog::create([ 'recipient_type' => $recipientType, 'recipient_id' => $recipientId, 'recipient_email' => $email, 'notification_type' => $notificationType, 'mailable_class' => $mailable::class, 'job_listing_id' => $jobListingId, 'candidate_id' => $candidateId, 'dedupe_key' => $dedupeKey, 'status' => EmailSendLog::STATUS_QUEUED, ]); $payload = $this->extractMailablePayload($mailable); SendTalentBankEmailJob::dispatch( $log->id, $email, $mailable::class, $payload, )->onQueue(config('talent_bank.notification_queue', 'notifications')); } public function markSent(EmailSendLog $log): void { $log->update([ 'status' => EmailSendLog::STATUS_SENT, 'sent_at' => now(), ]); } public function markFailed(EmailSendLog $log, string $message): void { $log->update([ 'status' => EmailSendLog::STATUS_FAILED, 'error_message' => $message, ]); } private function wasRecentlySent(string $dedupeKey): bool { return EmailSendLog::query() ->where('dedupe_key', $dedupeKey) ->whereIn('status', [EmailSendLog::STATUS_QUEUED, EmailSendLog::STATUS_SENT]) ->where('created_at', '>=', now()->subMinutes(config('talent_bank.email_dedupe_minutes', 10))) ->exists(); } /** * @return array<int, mixed> */ private function extractMailablePayload(Mailable $mailable): array { if ($mailable instanceof \App\Mail\TalentBank\ApplicationEventMail) { return [ $mailable->notificationType, $mailable->audience, $mailable->job, $mailable->candidate, $mailable->context, ]; } return []; } private function logSkipped( string $notificationType, string $recipientType, ?int $recipientId, string $email, Mailable $mailable, string $dedupeKey, ?int $jobListingId, ?int $candidateId, ): void { EmailSendLog::create([ 'recipient_type' => $recipientType, 'recipient_id' => $recipientId, 'recipient_email' => $email, 'notification_type' => $notificationType, 'mailable_class' => $mailable::class, 'job_listing_id' => $jobListingId, 'candidate_id' => $candidateId, 'dedupe_key' => $dedupeKey, 'status' => EmailSendLog::STATUS_SKIPPED_DUPLICATE, ]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings