Skip to content

Instantly share code, notes, and snippets.

@bakura10
Created July 25, 2012 11:46
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/3175711 to your computer and use it in GitHub Desktop.
Save bakura10/3175711 to your computer and use it in GitHub Desktop.
Authentication Factory
<?php
namespace Common\Authentication\Service;
use DoctrineModule\Authentication\Adapter\ObjectRepository as DoctrineAdapter;
use DoctrineModule\Authentication\Storage\ObjectRepository as DoctrineStorage;
use DoctrineModule\Options\Authentication as AuthenticationOptions;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ConfigFactory 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');
$metadataFactory = $em->getMetadataFactory();
$options->setObjectManager($em);
$storage = new DoctrineStorage($options->getObjectRepository(), $metadataFactory, new SessionStorage());
$adapter = new DoctrineAdapter($options);
$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