/**
* Recovers the $trashedLocation at its original place if possible.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
*
* If $newParentLocation is provided, $trashedLocation will be restored under it.
*
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
*/
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);
}