Rabbi_Gerzi/backend/app/Set/EloquentSetRepository.php
2026-07-02 17:10:23 +03:00

76 lines
1.8 KiB
PHP

<?php
namespace App\Set;
use DomainException;
class EloquentSetRepository implements SetRepository
{
public function create(CreateSetDto $dto): Set
{
$model = SetModel::create([
'name' => $dto->name,
'description' => $dto->description,
'icon_image_url' => $dto->iconImageUrl,
]);
return $this->toDomain($model);
}
public function update(Set $set): Set
{
$model = SetModel::find($set->getId());
if ($model === null) {
throw new DomainException(
"Set with id: {$set->getId()} doesnt exist"
);
}
$model->name = $set->getName();
$model->description = $set->getDescription();
$model->icon_image_url = $set->getIconImageUrl();
$model->save();
return $this->toDomain($model);
}
public function delete(Set $set): void
{
$model = SetModel::find($set->getId());
if ($model === null) {
throw new DomainException(
"Set with id: {$set->getId()} doesnt exist"
);
}
$model->delete();
}
public function find(int $id): ?Set
{
$model = SetModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
public function getAll(): array
{
$models = SetModel::orderBy('id')->get();
$sets = [];
foreach ($models as $model) {
$sets[] = $this->toDomain($model);
}
return $sets;
}
private function toDomain(SetModel $model): Set
{
return new Set(
id: $model->id,
name: $model->name,
description: $model->description,
iconImageUrl: $model->icon_image_url,
);
}
}