eZ\Publish\Core\Repository\TrashService::recover PHP Method

recover() public method

Recovers the $trashedLocation at its original place if possible.
public recover ( eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem, eZ\Publish\API\Repository\Values\Content\Location $newParentLocation = null ) : eZ\Publish\API\Repository\Values\Content\Location
$trashItem eZ\Publish\API\Repository\Values\Content\TrashItem
$newParentLocation eZ\Publish\API\Repository\Values\Content\Location
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);
    }