enforce element parent scope
This commit is contained in:
parent
eccfed2350
commit
588a3ecb40
3 changed files with 43 additions and 0 deletions
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue