/** * Updates $tag. * * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified tag is not found * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to update this tag * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the remote ID already exists * * @param \Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag * @param \Netgen\TagsBundle\API\Repository\Values\Tags\TagUpdateStruct $tagUpdateStruct * * @return \Netgen\TagsBundle\API\Repository\Values\Tags\Tag The updated tag */ public function updateTag(Tag $tag, TagUpdateStruct $tagUpdateStruct) { $keywords = $tagUpdateStruct->getKeywords(); if ($tag->mainTagId > 0) { if ($this->hasAccess('tags', 'edit') !== true) { throw new UnauthorizedException('tags', 'edit'); } } else { if ($this->hasAccess('tags', 'editsynonym') !== true) { throw new UnauthorizedException('tags', 'editsynonym'); } } if ($keywords !== null && (!is_array($keywords) || empty($keywords))) { throw new InvalidArgumentValue('keywords', $keywords, 'TagUpdateStruct'); } if ($keywords !== null) { foreach ($keywords as $keyword) { if (empty($keyword)) { throw new InvalidArgumentValue('keywords', $keywords, 'TagUpdateStruct'); } } } if ($tagUpdateStruct->remoteId !== null && (!is_string($tagUpdateStruct->remoteId) || empty($tagUpdateStruct->remoteId))) { throw new InvalidArgumentValue('remoteId', $tagUpdateStruct->remoteId, 'TagUpdateStruct'); } $spiTag = $this->tagsHandler->load($tag->id); if ($tagUpdateStruct->remoteId !== null) { try { $existingTag = $this->tagsHandler->loadTagInfoByRemoteId($tagUpdateStruct->remoteId); if ($existingTag->id !== $spiTag->id) { throw new InvalidArgumentException('tagUpdateStruct', 'Tag with provided remote ID already exists'); } } catch (NotFoundException $e) { // Do nothing } } if ($tagUpdateStruct->mainLanguageCode !== null && (!is_string($tagUpdateStruct->mainLanguageCode) || empty($tagUpdateStruct->mainLanguageCode))) { throw new InvalidArgumentValue('mainLanguageCode', $tagUpdateStruct->mainLanguageCode, 'TagUpdateStruct'); } $mainLanguageCode = $spiTag->mainLanguageCode; if ($tagUpdateStruct->mainLanguageCode !== null) { $mainLanguageCode = $tagUpdateStruct->mainLanguageCode; } $newKeywords = $spiTag->keywords; if ($keywords !== null) { $newKeywords = $keywords; } if (!isset($newKeywords[$mainLanguageCode])) { throw new InvalidArgumentValue('mainLanguageCode', $tagUpdateStruct->mainLanguageCode, 'TagUpdateStruct'); } if ($tagUpdateStruct->alwaysAvailable !== null && !is_bool($tagUpdateStruct->alwaysAvailable)) { throw new InvalidArgumentValue('alwaysAvailable', $tagUpdateStruct->alwaysAvailable, 'TagUpdateStruct'); } $updateStruct = new UpdateStruct(); $updateStruct->keywords = $newKeywords !== null ? $newKeywords : $spiTag->keywords; $updateStruct->remoteId = $tagUpdateStruct->remoteId !== null ? trim($tagUpdateStruct->remoteId) : $spiTag->remoteId; $updateStruct->mainLanguageCode = $tagUpdateStruct->mainLanguageCode !== null ? trim($tagUpdateStruct->mainLanguageCode) : $spiTag->mainLanguageCode; $updateStruct->alwaysAvailable = $tagUpdateStruct->alwaysAvailable !== null ? $tagUpdateStruct->alwaysAvailable : $spiTag->alwaysAvailable; $this->repository->beginTransaction(); try { $updatedTag = $this->tagsHandler->update($updateStruct, $spiTag->id); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } return $this->buildTagDomainObject($updatedTag); }