service-captcha/app/Repositories/CaptchaTokenRepository.php
Leonid Nikitin 27046e6674
Revived API /api/v1/captcha.
Now a new captcha is created to check for a bot.
2023-09-19 14:27:33 +06:00

45 lines
1.3 KiB
PHP

<?php declare(strict_types=1);
namespace App\Repositories;
use App\Contracts\Search;
use App\Models\CaptchaToken;
use App\Services\CaptchaToken\BuilderCommand;
use App\Services\Search\CreateSearchInstanceCommand;
use App\Dto\Builder\CaptchaToken as CaptchaTokenDto;
use Illuminate\Database\Eloquent\Builder;
final readonly class CaptchaTokenRepository
{
public function __construct(
private CreateSearchInstanceCommand $createSearchInstanceCommand,
private BuilderCommand $builderCommand
) { }
public function getCaptchaTokenById(int $id): ?CaptchaToken
{
return CaptchaToken::query()->where('id', $id)->first();
}
public function getCaptchaTokens(CaptchaTokenDto $captchaTokenDto, array $with = [], ?int $userId = null): Search
{
$query = CaptchaToken::query()
->when($userId, function (Builder $query, int $userId) {
$query->where('user_id', $userId);
})
->with($with);
$query = $this->builderCommand->execute(
query: $query,
captchaTokenDto: $captchaTokenDto
);
return $this->createSearchInstanceCommand->execute($query);
}
public function getCaptchaTokenByPublicToken(string $token): ?CaptchaToken
{
return CaptchaToken::query()->where('public_token', '=', $token)->first();
}
}