/** * Converts $tag to a synonym of $mainTag. * * @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 convert tag to synonym * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If either one of the tags is a synonym * If the main tag is a sub tag of the given tag * * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $mainTag * * @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag The converted synonym */ public function convertToSynonym(Tag $tag, Tag $mainTag) { if ($this->hasAccess('tags', 'makesynonym') !== true) { throw new UnauthorizedException('tags', 'makesynonym'); } $spiTagInfo = $this->tagsHandler->loadTagInfo($tag->id); $spiMainTagInfo = $this->tagsHandler->loadTagInfo($mainTag->id); if ($spiTagInfo->mainTagId > 0) { throw new InvalidArgumentException('tag', 'Source tag is a synonym'); } if ($spiMainTagInfo->mainTagId > 0) { throw new InvalidArgumentException('mainTag', 'Destination tag is a synonym'); } if (strpos($spiMainTagInfo->pathString, $spiTagInfo->pathString) === 0) { throw new InvalidArgumentException('mainTag', 'Destination tag is a sub tag of the given tag'); } $this->repository->beginTransaction(); try { foreach ($this->tagsHandler->loadChildren($spiTagInfo->id) as $child) { $this->tagsHandler->moveSubtree($child->id, $spiMainTagInfo->id); } $convertedTag = $this->tagsHandler->convertToSynonym($spiTagInfo->id, $spiMainTagInfo->id); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->buildTagDomainObject($convertedTag); }