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