public
Created

Auth Storage adapter for Zend Framework and Doctrine 2

  • Download Gist
Session.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.