public function recover(APITrashItem $trashItem, Location $newParentLocation = null)
{
if (!is_numeric($trashItem->id)) {
throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem');
}
if ($newParentLocation === null && !is_numeric($trashItem->parentLocationId)) {
throw new InvalidArgumentValue('parentLocationId', $trashItem->parentLocationId, 'TrashItem');
}
if ($newParentLocation !== null && !is_numeric($newParentLocation->id)) {
throw new InvalidArgumentValue('parentLocationId', $newParentLocation->id, 'Location');
}
if ($this->repository->hasAccess('content', 'restore') !== true) {
throw new UnauthorizedException('content', 'restore');
}
$this->repository->beginTransaction();
try {
$newParentLocationId = $newParentLocation ? $newParentLocation->id : $trashItem->parentLocationId;
$newLocationId = $this->persistenceHandler->trashHandler()->recover($trashItem->id, $newParentLocationId);
$content = $this->repository->getContentService()->loadContent($trashItem->contentId);
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content);
// Publish URL aliases for recovered location
foreach ($urlAliasNames as $languageCode => $name) {
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation($newLocationId, $newParentLocationId, $name, $languageCode, $content->contentInfo->alwaysAvailable);
}
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->repository->getLocationService()->loadLocation($newLocationId);
}