/** * Creates a synonym for $tag. * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to create a synonym * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the target tag is a synonym * * @param \Netgen\TagsBundle\API\Repository\Values\Tags\SynonymCreateStruct $synonymCreateStruct * * @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag The created synonym */ public function addSynonym(SynonymCreateStruct $synonymCreateStruct) { $keywords = $synonymCreateStruct->getKeywords(); if ($this->hasAccess('tags', 'addsynonym') !== true) { throw new UnauthorizedException('tags', 'addsynonym'); } $mainTag = $this->tagsHandler->loadTagInfo($synonymCreateStruct->mainTagId); if ($mainTag->mainTagId > 0) { throw new InvalidArgumentValue('mainTagId', $synonymCreateStruct->mainTagId, 'SynonymCreateStruct'); } if (empty($synonymCreateStruct->mainLanguageCode) || !is_string($synonymCreateStruct->mainLanguageCode)) { throw new InvalidArgumentValue('mainLanguageCode', $synonymCreateStruct->mainLanguageCode, 'SynonymCreateStruct'); } if (empty($keywords) || !is_array($keywords)) { throw new InvalidArgumentValue('keywords', $keywords, 'SynonymCreateStruct'); } if (!isset($keywords[$synonymCreateStruct->mainLanguageCode])) { throw new InvalidArgumentValue('keywords', $keywords, 'SynonymCreateStruct'); } if ($synonymCreateStruct->remoteId !== null && (empty($synonymCreateStruct->remoteId) || !is_string($synonymCreateStruct->remoteId))) { throw new InvalidArgumentValue('remoteId', $synonymCreateStruct->remoteId, 'SynonymCreateStruct'); } // check for existence of tag with provided remote ID if ($synonymCreateStruct->remoteId !== null) { try { $this->tagsHandler->loadTagInfoByRemoteId($synonymCreateStruct->remoteId); throw new InvalidArgumentException('synonymCreateStruct', 'Tag with provided remote ID already exists'); } catch (NotFoundException $e) { // Do nothing } } else { $synonymCreateStruct->remoteId = md5(uniqid(get_class($this), true)); } if (!is_bool($synonymCreateStruct->alwaysAvailable)) { throw new InvalidArgumentValue('alwaysAvailable', $synonymCreateStruct->alwaysAvailable, 'SynonymCreateStruct'); } $createStruct = new SPISynonymCreateStruct(); $createStruct->mainTagId = $synonymCreateStruct->mainTagId; $createStruct->mainLanguageCode = $synonymCreateStruct->mainLanguageCode; $createStruct->keywords = $keywords; $createStruct->remoteId = $synonymCreateStruct->remoteId; $createStruct->alwaysAvailable = $synonymCreateStruct->alwaysAvailable; $this->repository->beginTransaction(); try { $newTag = $this->tagsHandler->addSynonym($createStruct); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->buildTagDomainObject($newTag); }