mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Add providers factory
This commit is contained in:
43
lib/Alchemy/Phrasea/Authentication/Provider/Factory.php
Normal file
43
lib/Alchemy/Phrasea/Authentication/Provider/Factory.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@@ -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'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user