enforce element parent scope

This commit is contained in:
Yisroel Baum 2026-08-08 22:22:25 +03:00
parent eccfed2350
commit 588a3ecb40
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 43 additions and 0 deletions

View file

@ -2,8 +2,13 @@
namespace App\Element;
use DomainException;
interface ElementRepository
{
/**
* @throws DomainException
*/
public function create(CreateElementDto $dto): Element;
public function find(int $id): ?Element;

View file

@ -4,6 +4,7 @@ namespace App\Element;
use App\Set\Set;
use App\Set\SetRepository;
use DomainException;
use RuntimeException;
class EloquentElementRepository implements ElementRepository
@ -14,6 +15,7 @@ class EloquentElementRepository implements ElementRepository
public function create(CreateElementDto $dto): Element
{
$this->validateParentSet($dto);
$position = $this->nextPosition(
$dto->set,
$dto->parentElement,
@ -43,6 +45,23 @@ class EloquentElementRepository implements ElementRepository
return $model === null ? null : $this->toDomain($model);
}
/**
* @throws DomainException
*/
private function validateParentSet(CreateElementDto $dto): void
{
$parentElement = $dto->parentElement;
if ($parentElement === null) {
return;
}
if ($parentElement->getSet()->getId() !== $dto->set->getId()) {
throw new DomainException(
'parent element must belong to the same set',
);
}
}
private function nextPosition(
Set $set,
?Element $parentElement,