/** * Copies the subtree starting from $tag as a new subtree of $targetParentTag. * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If either of specified tags is not found * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to read tags * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the target tag is a sub tag of the given tag * If the target tag is already a parent of the given tag * If either one of the tags is a synonym * * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag The subtree denoted by the tag to copy * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $targetParentTag The target parent tag for the copy operation * * @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag The newly created tag of the copied subtree */ public function copySubtree(Tag $tag, Tag $targetParentTag) { if ($this->hasAccess('tags', 'read') !== true) { throw new UnauthorizedException('tags', 'read'); } $spiTagInfo = $this->tagsHandler->loadTagInfo($tag->id); $spiParentTagInfo = $this->tagsHandler->loadTagInfo($targetParentTag->id); if ($spiTagInfo->mainTagId > 0) { throw new InvalidArgumentException('tag', 'Source tag is a synonym'); } if ($spiParentTagInfo->mainTagId > 0) { throw new InvalidArgumentException('targetParentTag', 'Target parent tag is a synonym'); } if ($tag->parentTagId == $targetParentTag->id) { throw new InvalidArgumentException('targetParentTag', 'Target parent tag is already the parent of the given tag'); } if (strpos($spiParentTagInfo->pathString, $spiTagInfo->pathString) === 0) { throw new InvalidArgumentException('targetParentTag', 'Target parent tag is a sub tag of the given tag'); } $this->repository->beginTransaction(); try { $copiedTag = $this->tagsHandler->copySubtree($spiTagInfo->id, $spiParentTagInfo->id); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->buildTagDomainObject($copiedTag); }