2023-09-19 14:27:33 +06:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
2023-11-26 15:09:42 +06:00
|
|
|
use App\Dto\Repository\DataCaptchaRepository\DataCaptcha;
|
2023-09-19 14:27:33 +06:00
|
|
|
use App\Exceptions\Repositories\DataCaptchaRepositoryException;
|
|
|
|
use App\Models\Captcha;
|
|
|
|
use App\Services\GenerateTokenCommand\GenerateTokenUlidCommand;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
final readonly class DataCaptchaRepository
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private GenerateTokenUlidCommand $generateTokenUlidCommand,
|
|
|
|
) { }
|
|
|
|
|
|
|
|
public function store(Captcha $captcha, array $coordinators, Carbon $expires): string
|
|
|
|
{
|
|
|
|
$key = $this->generateTokenUlidCommand->execute() . '_' . time();
|
|
|
|
$data = [
|
|
|
|
'id' => $captcha->id,
|
|
|
|
'coordinators' => $coordinators
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!Cache::driver('redis')->add($key, $data, $expires) ) {
|
|
|
|
throw new DataCaptchaRepositoryException('Could not create cache for captcha: ' . $captcha->id . ' key: ' . $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
2023-11-26 15:09:42 +06:00
|
|
|
|
|
|
|
public function getByKey(string $key): ?DataCaptcha
|
|
|
|
{
|
|
|
|
$dataCaptcha = Cache::driver('redis')->get($key);
|
|
|
|
if (is_null($dataCaptcha)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataCaptcha(
|
|
|
|
captchaId: $dataCaptcha['id'],
|
|
|
|
coordinators: $dataCaptcha['coordinators'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(string $key): void
|
|
|
|
{
|
|
|
|
Cache::driver('redis')->delete($key);
|
|
|
|
}
|
2023-09-19 14:27:33 +06:00
|
|
|
}
|