complete story 11.2 with qa tests
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Consultation;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class GuestBookingSubmittedMail extends Mailable implements ShouldQueue
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct(public Consultation $consultation)
|
||||
{
|
||||
$this->locale = session('locale', 'ar');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
$locale = session('locale', 'ar');
|
||||
|
||||
return new Envelope(
|
||||
subject: $locale === 'ar'
|
||||
? 'تم استلام طلب الاستشارة'
|
||||
: 'Your Consultation Request Has Been Submitted',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
$locale = session('locale', 'ar');
|
||||
|
||||
return new Content(
|
||||
markdown: 'emails.booking.guest-submitted.'.$locale,
|
||||
with: [
|
||||
'consultation' => $this->consultation,
|
||||
'guestName' => $this->consultation->guest_name,
|
||||
'summaryPreview' => $this->getSummaryPreview(),
|
||||
'formattedDate' => $this->getFormattedDate($locale),
|
||||
'formattedTime' => $this->getFormattedTime(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get truncated summary preview (max 200 characters).
|
||||
*/
|
||||
public function getSummaryPreview(): string
|
||||
{
|
||||
$summary = $this->consultation->problem_summary ?? '';
|
||||
|
||||
return strlen($summary) > 200
|
||||
? substr($summary, 0, 200).'...'
|
||||
: $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted date based on locale.
|
||||
*/
|
||||
public function getFormattedDate(string $locale): string
|
||||
{
|
||||
$date = $this->consultation->booking_date;
|
||||
|
||||
return $locale === 'ar'
|
||||
? $date->format('d/m/Y')
|
||||
: $date->format('m/d/Y');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted time.
|
||||
*/
|
||||
public function getFormattedTime(): string
|
||||
{
|
||||
$time = $this->consultation->booking_time;
|
||||
|
||||
return Carbon::parse($time)->format('h:i A');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachments for the message.
|
||||
*
|
||||
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class CaptchaService
|
||||
{
|
||||
private const SESSION_KEY = 'captcha_answer';
|
||||
|
||||
/**
|
||||
* Generate a new math captcha question.
|
||||
*
|
||||
* @return array{question: string, question_ar: string}
|
||||
*/
|
||||
public function generate(): array
|
||||
{
|
||||
$num1 = rand(1, 10);
|
||||
$num2 = rand(1, 10);
|
||||
$answer = $num1 + $num2;
|
||||
|
||||
session([self::SESSION_KEY => $answer]);
|
||||
|
||||
return [
|
||||
'question' => "What is {$num1} + {$num2}?",
|
||||
'question_ar' => "ما هو {$num1} + {$num2}؟",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the user's captcha answer.
|
||||
*/
|
||||
public function validate(mixed $answer): bool
|
||||
{
|
||||
$expected = session(self::SESSION_KEY);
|
||||
|
||||
if (is_null($expected)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (int) $answer === (int) $expected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the current captcha from session.
|
||||
*/
|
||||
public function clear(): void
|
||||
{
|
||||
session()->forget(self::SESSION_KEY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user