my-projects-website/app/application/app/Repositories/ProjectRepository.php

55 lines
1.6 KiB
PHP

<?php declare(strict_types=1);
namespace App\Repositories;
use App\Contracts\Search;
use App\Dto\Builder\Project as ProjectBuilderDto;
use App\Models\Project;
use App\Services\Project\BuilderCommand;
use App\Services\Search\CreateSearchInstanceCommand;
use Illuminate\Database\Eloquent\Builder;
final readonly class ProjectRepository
{
public function __construct(
private CreateSearchInstanceCommand $createSearchInstanceCommand,
private BuilderCommand $builderCommand
) { }
public function getProjects(ProjectBuilderDto $projectBuilderDto, array $with = []): Search
{
$query = $this->builderCommand->execute(
query: Project::query()->with($with),
projectBuilderDto: $projectBuilderDto
);
return $this->createSearchInstanceCommand->execute($query);
}
public function getProjectByHttpHost(string $httpHost): ?Project
{
return Project::query()->where('http_host', $httpHost)->first();
}
public function getProjectById(int $id): ?Project
{
return Project::query()->where('id', $id)->first();
}
public function getProjectByCode(string $code): ?Project
{
return Project::query()->where('code', $code)->first();
}
public function isExistsCode(string $code, ?int $exceptId = null): bool
{
return Project::query()
->where('code', $code)
->when($exceptId, function (Builder $query, int $exceptId) {
$query->where('id', '!=', $exceptId);
})
->withTrashed()
->exists();
}
}