Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Added VersionNotFoundException #107

Merged
merged 7 commits into from
Mar 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/DocumentManagerInterface.php
Original file line number Diff line number Diff line change
@@ -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 = []);
3 changes: 3 additions & 0 deletions lib/Exception/InvalidLocaleException.php
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ class InvalidLocaleException extends \InvalidArgumentException
*/
private $locale;

/**
* @param string $locale
*/
public function __construct($locale)
{
parent::__construct(sprintf('Invalid locale "%s"', $locale));
3 changes: 3 additions & 0 deletions lib/Exception/NodeNameAlreadyExistsException.php
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ class NodeNameAlreadyExistsException extends DocumentManagerException
*/
private $nodeName;

/**
* @param string $nodeName
*/
public function __construct($nodeName)
{
parent::__construct(
58 changes: 58 additions & 0 deletions lib/Exception/VersionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to not have such a small exception, but a parameterized one. So that it can be used like throw new VersionNotFoundException($document, $version), and it generates the message on its own.

{
/**
* @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;
}
}
28 changes: 22 additions & 6 deletions lib/Subscriber/Behavior/VersionSubscriber.php
Original file line number Diff line number Diff line change
@@ -14,13 +14,15 @@
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;
use Sulu\Component\DocumentManager\Event\PersistEvent;
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