complete story 11.2 with qa tests

This commit is contained in:
Naser Mansour
2026-01-03 19:15:07 +02:00
parent f32ea2b68d
commit 06ece9f4b2
12 changed files with 1116 additions and 14 deletions
+49
View File
@@ -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);
}
}