Skip to content

Commit

Permalink
Abstract out ::_loadState() and move ::loadState to base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Oct 3, 2013
1 parent 5b03611 commit d97801f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 116 deletions.
73 changes: 65 additions & 8 deletions framework/ActiveSync/lib/Horde/ActiveSync/State/Base.php
Expand Up @@ -447,17 +447,74 @@ static public function RowCmp($a, $b)
}

/**
* Loads the state from storage for the specified syncKey and intializes the
* object for use.
* Load and initialize the sync state
*
* @param array $collection The collection array, if loading a collection.
* @param string $syncKey The key for the state to load.
* @param string $type Treat the loaded state data as this type of state.
* @param string $id The collection id this represents
* @param array $collection The collection array for the collection, if
* a FOLDERSYNC, pass an empty array.
* @param string $syncKey The synckey of the state to load. If empty will
* force a reset of the state for the class
* specified in $id
* @param string $type The type of state a
* Horde_ActiveSync::REQUEST_TYPE constant.
* @param string $id The folder id this state represents. If empty
* assumed to be a foldersync state.
*
* @return array The state array
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StateGone
*/
abstract public function loadState(array $collection, $syncKey, $type = null, $id = null);
public function loadState(array $collection, $syncKey, $type = null, $id = null)
{
// Initialize the local members.
$this->_collection = $collection;
$this->_changes = null;
$this->_type = $type;

// If this is a FOLDERSYNC, mock the device id.
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC && empty($id)) {
$id = Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC;
}

// synckey == 0 is an initial sync or reset.
if (empty($syncKey)) {
$this->_logger->notice(sprintf(
'[%s] %s::loadState: clearing folder state.',
$this->_procid,
__CLASS__));
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC) {
$this->_folder = array();
} else {
// Create a new folder object.
$this->_folder = ($this->_collection['class'] == Horde_ActiveSync::CLASS_EMAIL) ?
new Horde_ActiveSync_Folder_Imap($this->_collection['serverid'], Horde_ActiveSync::CLASS_EMAIL) :
new Horde_ActiveSync_Folder_Collection($this->_collection['serverid'], $this->_collection['class']);
}
$this->_syncKey = '0';
$this->_resetDeviceState($id);
return;
}

$this->_logger->info(
sprintf('[%s] Loading state for synckey %s',
$this->_procid,
$syncKey)
);

// Check if synckey is allowed
if (!preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $syncKey, $matches)) {
throw new Horde_ActiveSync_Exception('Invalid sync key');
}
$this->_syncKey = $syncKey;

// Cleanup older syncstates
$this->_gc($syncKey);

// Load the state
$this->_loadState($type);
}

protected function _loadState($type)
{
throw new Horde_ActiveSync_Exception('Not implemented.');
}

/**
* Get the list of known folders for the specified syncState
Expand Down
55 changes: 2 additions & 53 deletions framework/ActiveSync/lib/Horde/ActiveSync/State/Mongo.php
Expand Up @@ -127,65 +127,15 @@ public function updateServerIdInState($uid, $serverid)
}

/**
* Load and initialize the sync state
* Load the state represented by $syncKey from storage.
*
* @param array $collection The collection array for the collection, if
* a FOLDERSYNC, pass an empty array.
* @param string $syncKey The synckey of the state to load. If empty will
* force a reset of the state for the class
* specified in $id
* @param string $type The type of state a
* Horde_ActiveSync::REQUEST_TYPE constant.
* @param string $id The folder id this state represents. If empty
* assumed to be a foldersync state.
*
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StateGone
*/
public function loadState(array $collection, $syncKey, $type = null, $id = null)
protected function _loadState($type)
{
// Initialize the local members.
$this->_collection = $collection;
$this->_changes = null;
$this->_type = $type;

// If this is a FOLDERSYNC, mock the device id.
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC && empty($id)) {
$id = Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC;
}

// synckey == 0 is an initial sync or reset.
if (empty($syncKey)) {
$this->_logger->notice(sprintf(
'[%s] Horde_ActiveSync_State_Mongo::loadState: clearing folder state.',
$this->_procid));
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC) {
$this->_folder = array();
} else {
// Create a new folder object.
$this->_folder = ($this->_collection['class'] == Horde_ActiveSync::CLASS_EMAIL) ?
new Horde_ActiveSync_Folder_Imap($this->_collection['serverid'], Horde_ActiveSync::CLASS_EMAIL) :
new Horde_ActiveSync_Folder_Collection($this->_collection['serverid'], $this->_collection['class']);
}
$this->_syncKey = '0';
$this->_resetDeviceState($id);
return;
}

$this->_logger->info(
sprintf('[%s] Loading state for synckey %s',
$this->_procid,
$syncKey)
);

// Check if synckey is allowed
if (!preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $syncKey, $matches)) {
throw new Horde_ActiveSync_Exception('Invalid sync key');
}
$this->_syncKey = $syncKey;

// Cleanup older syncstates
$this->_gc($syncKey);

// Load the previous syncState from storage
try {
$results = $this->_db->state->findOne(
Expand All @@ -206,7 +156,6 @@ public function loadState(array $collection, $syncKey, $type = null, $id = null)

$this->_loadStateFromResults($results, $type);
}

/**
* Actually load the state data into the object from the query results.
*
Expand Down
59 changes: 4 additions & 55 deletions framework/ActiveSync/lib/Horde/ActiveSync/State/Sql.php
Expand Up @@ -186,70 +186,19 @@ public function updateServerIdInState($uid, $serverid)
}

/**
* Load and initialize the sync state
* Load the state represented by $syncKey from storage.
*
* @param array $collection The collection array for the collection, if
* a FOLDERSYNC, pass an empty array.
* @param string $syncKey The synckey of the state to load. If empty will
* force a reset of the state for the class
* specified in $id
* @param string $type The type of state a
* Horde_ActiveSync::REQUEST_TYPE constant.
* @param string $id The folder id this state represents. If empty
* assumed to be a foldersync state.
*
* @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_StateGone
*/
public function loadState(array $collection, $syncKey, $type = null, $id = null)
protected function _loadState($type)
{
// Initialize the local members.
$this->_collection = $collection;
$this->_changes = null;
$this->_type = $type;

// If this is a FOLDERSYNC, mock the device id.
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC && empty($id)) {
$id = Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC;
}

// synckey == 0 is an initial sync or reset.
if (empty($syncKey)) {
$this->_logger->notice(sprintf(
'[%s] Horde_ActiveSync_State_Sql::loadState: clearing folder state.',
$this->_procid));
if ($type == Horde_ActiveSync::REQUEST_TYPE_FOLDERSYNC) {
$this->_folder = array();
} else {
// Create a new folder object.
$this->_folder = ($this->_collection['class'] == Horde_ActiveSync::CLASS_EMAIL) ?
new Horde_ActiveSync_Folder_Imap($this->_collection['serverid'], Horde_ActiveSync::CLASS_EMAIL) :
new Horde_ActiveSync_Folder_Collection($this->_collection['serverid'], $this->_collection['class']);
}
$this->_syncKey = '0';
$this->_resetDeviceState($id);
return;
}

$this->_logger->info(
sprintf('[%s] Loading state for synckey %s',
$this->_procid,
$syncKey)
);

// Check if synckey is allowed
if (!preg_match('/^s{0,1}\{([0-9A-Za-z-]+)\}([0-9]+)$/', $syncKey, $matches)) {
throw new Horde_ActiveSync_Exception('Invalid sync key');
}

$this->_syncKey = $syncKey;

// Cleanup older syncstates
$this->_gc($syncKey);

// Load the previous syncState from storage
try {
$results = $this->_db->selectOne('SELECT sync_data, sync_devid, sync_mod, sync_pending FROM '
. $this->_syncStateTable . ' WHERE sync_key = ?', array($syncKey));
. $this->_syncStateTable . ' WHERE sync_key = ?', array($this->_syncKey));
} catch (Horde_Db_Exception $e) {
$this->_logger->err('Error in loading state from DB: ' . $e->getMessage());
throw new Horde_ActiveSync_Exception($e);
Expand All @@ -259,7 +208,7 @@ public function loadState(array $collection, $syncKey, $type = null, $id = null)
$this->_logger->err(sprintf(
'[%s] Could not find state for synckey %s.',
$this->_procid,
$syncKey));
$this->_syncKey));
throw new Horde_ActiveSync_Exception_StateGone();
}

Expand Down

0 comments on commit d97801f

Please sign in to comment.