-
-
Save vasildakov-zz/4bf970ab343b067238b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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