Ejemplo n.º 1
0
 /**
  * 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);
 }
Ejemplo n.º 2
0
 /**
  * @covers \Netgen\TagsBundle\Core\SignalSlot\TagsService::updateTag
  */
 public function testUpdateTag()
 {
     $tagUpdateStruct = new TagUpdateStruct();
     $tagUpdateStruct->alwaysAvailable = true;
     $tagUpdateStruct->setKeyword('netgen');
     $tag = new Tag(array('id' => 42, 'keywords' => array('eng-GB' => 'ez'), 'remoteId' => '123456', 'mainLanguageCode' => 'eng-GB', 'alwaysAvailable' => false));
     $this->tagsService->expects($this->once())->method('updateTag')->with($this->equalTo($tag), $this->equalTo($tagUpdateStruct))->will($this->returnValue(new Tag(array('id' => 42, 'keywords' => array('eng-GB' => 'netgen'), 'remoteId' => 123456, 'mainLanguageCode' => 'eng-GB', 'alwaysAvailable' => true))));
     $this->signalDispatcher->expects($this->once())->method('emit')->with($this->equalTo(new UpdateTagSignal(array('tagId' => 42, 'keywords' => array('eng-GB' => 'netgen'), 'remoteId' => '123456', 'mainLanguageCode' => 'eng-GB', 'alwaysAvailable' => true))));
     $signalSlotService = $this->getSignalSlotService();
     $updatedTag = $signalSlotService->updateTag($tag, $tagUpdateStruct);
     $this->assertInstanceOf('Netgen\\TagsBundle\\API\\Repository\\Values\\Tags\\Tag', $updatedTag);
     $this->assertEquals(42, $updatedTag->id);
     $this->assertEquals(array('eng-GB' => 'netgen'), $updatedTag->keywords);
     $this->assertEquals('123456', $updatedTag->remoteId);
     $this->assertEquals('eng-GB', $updatedTag->mainLanguageCode);
     $this->assertEquals(true, $updatedTag->alwaysAvailable);
 }