Add providers factory

This commit is contained in:
Romain Neutron
2013-03-15 17:10:33 +01:00
parent d86e4c4371
commit fce4affc71
2 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Authentication\Provider;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class Factory
{
private $generator;
private $session;
public function __construct(UrlGenerator $generator, SessionInterface $session)
{
$this->generator = $generator;
$this->session = $session;
}
public function build($name, array $options = array())
{
$name = implode('', array_map(function ($chunk) {
return ucfirst(strtolower($chunk));
}, explode('-', $name)));
$class_name = sprintf('%s\\%s', __NAMESPACE__, $name);
if (!class_exists($class_name)) {
throw new InvalidArgumentException(sprintf('Invalid provider %s', $name));
}
return $class_name::create($this->generator, $this->session, $options);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Alchemy\Tests\Phrasea\Authentication\Provider;
use Alchemy\Phrasea\Authentication\Provider\Factory;
class FactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideNameAndOptions
*/
public function testBuild($name, $options, $expected)
{
$generator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGenerator')
->disableOriginalConstructor()
->getMock();
$session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface');
$factory = new Factory($generator, $session);
$this->assertInstanceOf($expected, $factory->build($name, $options));
}
public function provideNameAndOptions()
{
return array(
array('github', array('client-id' => 'id', 'client-secret' => 'secret'), 'Alchemy\Phrasea\Authentication\Provider\Github'),
array('google-plus', array('client-id' => 'id', 'client-secret' => 'secret'), 'Alchemy\Phrasea\Authentication\Provider\GooglePlus'),
array('linkedin', array('client-id' => 'id', 'client-secret' => 'secret'), 'Alchemy\Phrasea\Authentication\Provider\Linkedin'),
array('twitter', array('consumer-key' => 'id', 'consumer-secret' => 'secret'), 'Alchemy\Phrasea\Authentication\Provider\Twitter'),
array('viadeo', array('client-id' => 'id', 'client-secret' => 'secret'), 'Alchemy\Phrasea\Authentication\Provider\Viadeo'),
);
}
}