Created
July 17, 2012 19:29
-
-
Save bakura10/3131456 to your computer and use it in GitHub Desktop.
Common\Authentication\Service\ConfigurationFactory
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 Common\Authentication\Service; | |
use Common\Authentication\Options\Configuration as AuthenticationOptions; | |
use Common\Authentication\Storage\Db as DbStorage; | |
use DoctrineModule\Authentication\Adapter\DoctrineObjectRepository as DoctrineAdapter; | |
use Zend\Authentication\AuthenticationService; | |
use Zend\Authentication\Storage\Session as SessionStorage; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class ConfigurationFactory implements FactoryInterface | |
{ | |
/** | |
* @param ServiceLocatorInterface $serviceLocator | |
* @return AuthenticationService | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
/** @var $options AuthenticationOptions */ | |
$options = $this->getOptions($serviceLocator); | |
$em = $serviceLocator->get('doctrine.entitymanager.orm_default'); | |
$objectRepository = $em->getRepository($options->getIdentityClass()); | |
$storage = new DbStorage($objectRepository, new SessionStorage()); | |
$adapter = new DoctrineAdapter($objectRepository, $options->getIdentityClass()); | |
$adapter->setIdentityProperty($options->getIdentityProperty()); | |
$adapter->setCredentialProperty($options->getCredentialProperty()); | |
if ($options->getIdentityCallable()) { | |
$adapter->setIdentityCallable($options->getIdentityCallable()); | |
} | |
if ($options->getCredentialCallable()) { | |
$adapter->setCredentialCallable($options->getCredentialCallable()); | |
} | |
$authenticationService = new AuthenticationService($storage, $adapter); | |
return $authenticationService; | |
} | |
/** | |
* @param ServiceLocatorInterface $serviceLocator | |
* @return AuthenticationOptions | |
* @throws \RuntimeException | |
*/ | |
public function getOptions(ServiceLocatorInterface $serviceLocator) | |
{ | |
$options = $serviceLocator->get('Configuration'); | |
$options = $options['authentication']; | |
if ($options === null) { | |
throw new \RuntimeException('Configuration could not be found in authentication'); | |
} | |
return new AuthenticationOptions($options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment