From fce4affc714d6a04d3e711c726d2047ebacaa184 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 15 Mar 2013 17:10:33 +0100 Subject: [PATCH] Add providers factory --- .../Authentication/Provider/Factory.php | 43 +++++++++++++++++++ .../Authentication/Provider/FactoryTest.php | 35 +++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Authentication/Provider/Factory.php create mode 100644 tests/Alchemy/Tests/Phrasea/Authentication/Provider/FactoryTest.php diff --git a/lib/Alchemy/Phrasea/Authentication/Provider/Factory.php b/lib/Alchemy/Phrasea/Authentication/Provider/Factory.php new file mode 100644 index 0000000000..bd868e3586 --- /dev/null +++ b/lib/Alchemy/Phrasea/Authentication/Provider/Factory.php @@ -0,0 +1,43 @@ +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); + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/Provider/FactoryTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/Provider/FactoryTest.php new file mode 100644 index 0000000000..fcdf2264b9 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Authentication/Provider/FactoryTest.php @@ -0,0 +1,35 @@ +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'), + ); + } +}