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