Skip to content

Instantly share code, notes, and snippets.

@havvg
Created July 7, 2012 12:35
Show Gist options
  • Save havvg/3066310 to your computer and use it in GitHub Desktop.
Save havvg/3066310 to your computer and use it in GitHub Desktop.
CraueFormFlowBundle MongoDBStorage
<?php
namespace Ormigo\Bundle\OrmigoBundle\Form\Storage;
use Craue\FormFlowBundle\Storage\StorageInterface;
/**
* This storage is implemented against native MongoDB.
*
* The benefit from this is the separated persistent of the form flow data from the session.
*
* We generate a form flow key, which is based on user and flow, not session.
* This key is always the same and thus allows to save the step data for a users progress.
*
* It's required, as a user may log off (destroying the session) and wants to continue later on.
*/
class MongoDBStorage implements StorageInterface
{
/**
* @var \MongoCollection
*/
protected $storage;
/**
* Construct a MongoDBStorage saving form flow step data.
*
* @param \MongoCollection $storage The collection to store step data into.
*/
public function __construct(\MongoCollection $storage)
{
$this->storage = $storage;
}
/**
* Store the given value under the given key.
*
* @param string $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->storage->update(array('key' => $key), array(
'key' => $key,
'value' => $value,
), array(
'upsert' => true,
'fsync' => true,
));
}
/**
* Retrieve the data stored under the given key.
*
* @param string $key
* @param mixed $default If there is no data given, this value will be returned.
*
* @return mixed
*/
public function get($key, $default = null)
{
if (null === $document = $this->storage->findOne(array('key' => $key))) {
return $default;
}
return $document['value'];
}
/**
* Check if data is stored for the given key.
*
* @param string $key
*
* @return boolean
*/
public function has($key)
{
return null !== $this->storage->findOne(array('key' => $key));
}
/**
* Delete the stored data of the given key.
*
* @param string $key
*/
public function remove($key)
{
$this->storage->remove(array('key' => $key));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment