Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created November 11, 2011 10:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Ocramius/1357662 to your computer and use it in GitHub Desktop.
Save Ocramius/1357662 to your computer and use it in GitHub Desktop.
Auth Storage adapter for Zend Framework and Doctrine 2
<?php
namespace Ocramius\Auth\Storage;
use Entity\Ocramius\Admin as AdminEntity,
Doctrine\ORM\EntityManager;
/**
* An @see \Zend_Auth_Storage_Session that handles @see AdminEntity items
* @author Marco Pivetta <ocramius@gmail.com>
*/
class Session extends \Zend_Auth_Storage_Session {
const ADMIN_NAMESPACE_DEFAULT = 'admin_session_storage';
/**
* @var EntityManager
*/
protected $_entityManager;
/**
* @param EntityManager $entityManager
* @param string $namespace
* @param string $member
*/
public function __construct(
EntityManager $entityManager,
$namespace = self::ADMIN_NAMESPACE_DEFAULT,
$member = self::MEMBER_DEFAULT
) {
$this->_entityManager = $entityManager;
parent::__construct($namespace, $member);
}
public function isEmpty() {
return parent::isEmpty() || $this->read() === null;
}
/**
* @param AdminEntity|null $contents
*/
public function write($contents) {
if($contents instanceof AdminEntity) {
$identifier = $this
->_entityManager
->getClassMetadata(AdminEntity::getClassName())
->getIdentifierValues($contents);
$contents = empty($identifier) ? null : $identifier;
}else if($contents !== null) {
throw new InvalidArgumentException(
'Persisted identity must be either an '
. AdminEntity::getClassName() . ' or null'
);
}
parent::write($contents);
}
/**
* @return AdminEntity|null
*/
public function read() {
$data = parent::read();
if(empty($data)) {
return null;
}
$data = $this
->_entityManager
->find(AdminEntity::getClassName(), $data);
if($data === null) {
parent::write(null);
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment