63 lines
1.3 KiB
PHP
63 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Set;
|
|
|
|
use App\User\UserRepository;
|
|
use RuntimeException;
|
|
|
|
class EloquentSetRepository implements SetRepository
|
|
{
|
|
public function __construct(
|
|
private UserRepository $userRepository,
|
|
) {}
|
|
|
|
public function create(CreateSetDto $dto): Set
|
|
{
|
|
$model = SetModel::create([
|
|
'name' => $dto->name,
|
|
'creator_id' => $dto->creator->getId(),
|
|
]);
|
|
|
|
return new Set(
|
|
id: $model->id,
|
|
name: $model->name,
|
|
creator: $dto->creator,
|
|
);
|
|
}
|
|
|
|
public function find(int $id): ?Set
|
|
{
|
|
$model = SetModel::find($id);
|
|
|
|
return $model === null ? null : $this->toDomain($model);
|
|
}
|
|
|
|
public function all(): array
|
|
{
|
|
$models = SetModel::query()
|
|
->orderBy('name')
|
|
->orderBy('id')
|
|
->get();
|
|
$sets = [];
|
|
|
|
foreach ($models as $model) {
|
|
$sets[] = $this->toDomain($model);
|
|
}
|
|
|
|
return $sets;
|
|
}
|
|
|
|
private function toDomain(SetModel $model): Set
|
|
{
|
|
$creator = $this->userRepository->find($model->creator_id);
|
|
if ($creator === null) {
|
|
throw new RuntimeException('set creator not found');
|
|
}
|
|
|
|
return new Set(
|
|
id: $model->id,
|
|
name: $model->name,
|
|
creator: $creator,
|
|
);
|
|
}
|
|
}
|