/**
* Move entity hierarchy to new parent or root
*
* @param Permission $permission permission instance
* @param string|null $to to prefix
* @return void
* @throws InvalidArgumentException
* @throws NoParentException
*/
public function move(Permission $permission, $to = null)
{
$toParent = $to !== null ? $this->repo->findByName($permission->siteKey, $to) : null;
if ($to !== null && $toParent === null || $toParent !== null && $permission->type != $toParent->type) {
throw new InvalidArgumentException(['arg' => $to]);
}
$parent = $permission->getParent();
if ($parent === null) {
if ($permission->getDepth() !== 1) {
throw new NoParentException();
}
$this->repo->affiliate($permission, $to);
} else {
$this->repo->foster($permission, $to);
}
}