service-captcha/app/Repositories/CaptchaTokenRepository.php

40 lines
1.2 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);
}
}