diff --git a/CHANGELOG.md b/CHANGELOG.md index db43dd3..b86d839 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ -CHANGELOG for Sulu -================== +CHANGELOG for Sulu Document Manager +=================================== + +* dev-master + * ENHANCEMENT #107 Added VersionNotFoundException * 0.9.0-RC1 * FEATURE #105 Fixed changed times for both workspaces diff --git a/lib/DocumentManagerInterface.php b/lib/DocumentManagerInterface.php index cab5adb..eba593b 100644 --- a/lib/DocumentManagerInterface.php +++ b/lib/DocumentManagerInterface.php @@ -115,6 +115,8 @@ public function removeDraft($document, $locale); * @param string $version The UUID of the version to restore * @param array $options * + * @throws Exception\VersionNotFoundException + * * @return mixed */ public function restore($document, $locale, $version, array $options = []); diff --git a/lib/Exception/InvalidLocaleException.php b/lib/Exception/InvalidLocaleException.php index ec13f6a..74c23cc 100644 --- a/lib/Exception/InvalidLocaleException.php +++ b/lib/Exception/InvalidLocaleException.php @@ -21,6 +21,9 @@ class InvalidLocaleException extends \InvalidArgumentException */ private $locale; + /** + * @param string $locale + */ public function __construct($locale) { parent::__construct(sprintf('Invalid locale "%s"', $locale)); diff --git a/lib/Exception/NodeNameAlreadyExistsException.php b/lib/Exception/NodeNameAlreadyExistsException.php index 6c7cd92..6165cf5 100644 --- a/lib/Exception/NodeNameAlreadyExistsException.php +++ b/lib/Exception/NodeNameAlreadyExistsException.php @@ -21,6 +21,9 @@ class NodeNameAlreadyExistsException extends DocumentManagerException */ private $nodeName; + /** + * @param string $nodeName + */ public function __construct($nodeName) { parent::__construct( diff --git a/lib/Exception/VersionNotFoundException.php b/lib/Exception/VersionNotFoundException.php new file mode 100644 index 0000000..99861ff --- /dev/null +++ b/lib/Exception/VersionNotFoundException.php @@ -0,0 +1,58 @@ +<?php + +/* + * This file is part of Sulu. + * + * (c) MASSIVE ART WebServices GmbH + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Sulu\Component\DocumentManager\Exception; + +class VersionNotFoundException extends DocumentManagerException +{ + /** + * @var object + */ + private $document; + + /** + * @var int + */ + private $version; + + /** + * @param object $document + * @param string $version + */ + public function __construct($document, $version) + { + parent::__construct( + sprintf('Version "%s" for document "%s" not found', $version, $document->getUuid()) + ); + $this->document = $document; + $this->version = $version; + } + + /** + * The document, which was tried to restore. + * + * @return object + */ + public function getDocument() + { + return $this->document; + } + + /** + * The version, which was tried to restore. + * + * @return string + */ + public function getVersion() + { + return $this->version; + } +} diff --git a/lib/Subscriber/Behavior/VersionSubscriber.php b/lib/Subscriber/Behavior/VersionSubscriber.php index b44b7d6..3e611d3 100644 --- a/lib/Subscriber/Behavior/VersionSubscriber.php +++ b/lib/Subscriber/Behavior/VersionSubscriber.php @@ -14,6 +14,7 @@ use Jackalope\Version\VersionManager; use PHPCR\NodeInterface; use PHPCR\SessionInterface; +use PHPCR\Version\VersionException; use Sulu\Component\DocumentManager\Behavior\VersionBehavior; use Sulu\Component\DocumentManager\Event\AbstractMappingEvent; use Sulu\Component\DocumentManager\Event\HydrateEvent; @@ -21,6 +22,7 @@ use Sulu\Component\DocumentManager\Event\PublishEvent; use Sulu\Component\DocumentManager\Event\RestoreEvent; use Sulu\Component\DocumentManager\Events; +use Sulu\Component\DocumentManager\Exception\VersionNotFoundException; use Sulu\Component\DocumentManager\PropertyEncoder; use Sulu\Component\DocumentManager\Version; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -208,6 +210,8 @@ public function applyVersionOperations() * Restore the properties of the old version. * * @param RestoreEvent $event + * + * @throws VersionNotFoundException */ public function restoreProperties(RestoreEvent $event) { @@ -229,17 +233,29 @@ public function restoreProperties(RestoreEvent $event) } } - $version = $this->versionManager->getVersionHistory($node->getPath())->getVersion($event->getVersion()); - $frozenNode = $version->getFrozenNode(); + try { + $version = $this->versionManager->getVersionHistory($node->getPath())->getVersion($event->getVersion()); + + $frozenNode = $version->getFrozenNode(); - // set all the properties from the saved version to the node - foreach ($frozenNode->getPropertiesValues() as $name => $value) { - if ($this->isRestoreProperty($name, $contentPropertyPrefix, $systemPropertyPrefix)) { - $node->setProperty($name, $value); + // set all the properties from the saved version to the node + foreach ($frozenNode->getPropertiesValues() as $name => $value) { + if ($this->isRestoreProperty($name, $contentPropertyPrefix, $systemPropertyPrefix)) { + $node->setProperty($name, $value); + } } + } catch (VersionException $exception) { + throw new VersionNotFoundException($event->getDocument(), $event->getVersion()); } } + /** + * @param string $propertyName + * @param string $contentPrefix + * @param string $systemPrefix + * + * @return bool + */ private function isRestoreProperty($propertyName, $contentPrefix, $systemPrefix) { // return all localized and non-translatable properties