Skip to content

Instantly share code, notes, and snippets.

@bakura10
Created July 17, 2012 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakura10/3131459 to your computer and use it in GitHub Desktop.
Save bakura10/3131459 to your computer and use it in GitHub Desktop.
Common\Authentication\Storage\Db
<?php
namespace Common\Authentication\Storage;
use Doctrine\Common\Persistence\ObjectRepository;
use Zend\Authentication\Storage\StorageInterface;
/**
* This class implements StorageInterface and allow to save the result of an authentication against a database
*/
class Db implements StorageInterface
{
/**
* @var ObjectRepository
*/
protected $objectRepository;
/**
* @var StorageInterface
*/
protected $storage;
/**
* @param ObjectRepository $objectRepository
* @param StorageInterface $storage
*/
public function __construct(ObjectRepository $objectRepository, StorageInterface $storage)
{
$this->objectRepository = $objectRepository;
$this->storage = $storage;
}
/**
* @return bool
*/
public function isEmpty()
{
return $this->storage->isEmpty();
}
/**
* @return mixed|null|object
*/
public function read()
{
$identity = $this->storage->read();
if (is_scalar($identity)) {
return $this->objectRepository->find($identity);
} else {
return null;
}
}
/**
* @param $contents
* @return void
*/
public function write($contents)
{
$this->storage->write($contents);
}
/**
* @return void
*/
public function clear()
{
$this->storage->clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment