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

@ -5,6 +5,7 @@ namespace Tests\Fakes;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\ElementRepository;
use DomainException;
class FakeElementRepository implements ElementRepository
{
@ -15,6 +16,7 @@ class FakeElementRepository implements ElementRepository
public function create(CreateElementDto $dto): Element
{
$this->validateParentSet($dto);
$id = count($this->elements) + 1;
$element = new Element(
id: $id,
@ -36,6 +38,23 @@ class FakeElementRepository implements ElementRepository
return $element === null ? null : $this->copy($element);
}
/**
* @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(CreateElementDto $dto): int
{
$requestedParentId = $dto->parentElement?->getId();