service-captcha/app/Http/Controllers/Api/V1/CaptchaController.php

58 lines
2.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Http\Requests\Api\V1\Captcha\CaptchaRequest;
use App\Http\Requests\Api\V1\Captcha\CheckingRequest;
use App\Http\Requests\Api\V1\Captcha\VerificationInformationRequest;
use App\Http\Resources\Api\V1\Captcha;
use App\Http\Resources\Api\V1\CaptchaVerificationInformation;
use App\Http\Resources\Api\V1\CaptchaVerified;
use App\Services\Api\V1\CaptchaService;
use Illuminate\Http\JsonResponse;
final class CaptchaController extends Controller
{
public function __construct(
private readonly CaptchaService $captchaService
) { }
public function getCaptcha(CaptchaRequest $request): JsonResponse
{
$captchaPublicToken = $request->getDto();
$expires = now()->addSeconds(config('captcha.waiting_for_captcha_verification_in_seconds'));
$result = $this->captchaService->createKeyWithCaptcha($captchaPublicToken, $expires);
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new Captcha($result));
}
public function checking(CheckingRequest $request): JsonResponse
{
$maxCountError = config('captcha.validate_max_count_errors');
$params = $request->getDto();
$result = $this->captchaService->checking($params, $maxCountError);
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new CaptchaVerified($result));
}
public function verificationInformation(string $captchaUuid, VerificationInformationRequest $request): JsonResponse
{
$params = $request->getDto();
$expiresMinutes = config('captcha.verification_data_view_limit_in_minutes');
$maxInfoDisplayCount = config('captcha.max_info_display_count');
$result = $this->captchaService->verificationInformation($captchaUuid, $params, $expiresMinutes, $maxInfoDisplayCount);
if (!$result->isSuccess()) {
return response()->json($result->getData())->setStatusCode($result->getCode());
}
return response()->json(new CaptchaVerificationInformation($result));
}
}