Skip to content

Commit 5fe29ee

Browse files
committed
Add GatewayFactory registration methods
1 parent e5d4893 commit 5fe29ee

File tree

2 files changed

+72
-47
lines changed

2 files changed

+72
-47
lines changed

src/Omnipay/Common/GatewayFactory.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,59 @@
22

33
namespace Omnipay\Common;
44

5-
use RecursiveDirectoryIterator;
6-
use RecursiveIteratorIterator;
7-
use ReflectionClass;
85
use Guzzle\Http\ClientInterface;
96
use Omnipay\Common\Exception\RuntimeException;
107
use Symfony\Component\HttpFoundation\Request as HttpRequest;
118

129
class GatewayFactory
1310
{
14-
public static function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
15-
{
16-
$class = Helper::getGatewayClassName($class);
11+
private static $gateways = array();
1712

18-
if (!class_exists($class)) {
19-
throw new RuntimeException("Class '$class' not found");
20-
}
13+
/**
14+
* All available gateways
15+
*
16+
* @return array An array of gateway names
17+
*/
18+
public static function all()
19+
{
20+
return static::$gateways;
21+
}
2122

22-
$gateway = new $class($httpClient, $httpRequest);
23+
/**
24+
* Replace the list of available gateways
25+
*
26+
* @param array $gateways An array of gateway names
27+
*/
28+
public static function replace(array $gateways)
29+
{
30+
static::$gateways = $gateways;
31+
}
2332

24-
return $gateway;
33+
/**
34+
* Register a new gateway
35+
*
36+
* @param string $className Gateway name
37+
*/
38+
public static function register($className)
39+
{
40+
static::$gateways[] = $className;
2541
}
2642

2743
/**
28-
* Get a list of supported gateways, in friendly format (e.g. PayPal_Express)
44+
* Create a new gateway instance
45+
*
46+
* @param string $class Gateway name
47+
* @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation
48+
* @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation
2949
*/
30-
public static function find($directory = null)
50+
public static function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
3151
{
32-
$result = array();
33-
34-
// find all gateways in the Billing directory
35-
$directory = dirname(__DIR__);
36-
$it = new RecursiveDirectoryIterator($directory);
37-
foreach (new RecursiveIteratorIterator($it) as $file) {
38-
$filepath = $file->getPathName();
39-
if ('Gateway.php' === substr($filepath, -11)) {
40-
// determine class name
41-
$type = substr($filepath, 0, -11);
42-
$type = str_replace(array($directory, DIRECTORY_SEPARATOR), array('', '_'), $type);
43-
$type = trim($type, '_');
44-
$class = Helper::getGatewayClassName($type);
45-
46-
// ensure class exists and is not abstract
47-
if (class_exists($class)) {
48-
$reflection = new ReflectionClass($class);
49-
if (!$reflection->isAbstract() and
50-
$reflection->implementsInterface('\\Omnipay\\Common\\GatewayInterface')) {
51-
$result[] = $type;
52-
}
53-
}
54-
}
52+
$class = Helper::getGatewayClassName($class);
53+
54+
if (!class_exists($class)) {
55+
throw new RuntimeException("Class '$class' not found");
5556
}
5657

57-
return $result;
58+
return new $class($httpClient, $httpRequest);
5859
}
5960
}

tests/Omnipay/Common/GatewayFactoryTest.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,45 @@
22

33
namespace Omnipay\Common;
44

5+
use Mockery as m;
56
use Omnipay\TestCase;
67

78
class GatewayFactoryTest extends TestCase
89
{
9-
public function testCreate()
10+
public function tearDown()
1011
{
11-
$gateway = GatewayFactory::create('Stripe');
12-
$this->assertInstanceOf('\\Omnipay\\Stripe\\Gateway', $gateway);
12+
GatewayFactory::replace(array());
13+
}
14+
15+
public function testReplace()
16+
{
17+
$gateways = array('Foo');
18+
GatewayFactory::replace($gateways);
19+
20+
$this->assertSame($gateways, GatewayFactory::all());
21+
}
22+
23+
public function testRegister()
24+
{
25+
GatewayFactory::register('Bar');
26+
27+
$this->assertSame(array('Bar'), GatewayFactory::all());
28+
}
29+
30+
public function testCreateShortName()
31+
{
32+
m::mock('alias:Omnipay\\SpareChange\\BankGateway');
33+
34+
$gateway = GatewayFactory::create('SpareChange_Bank');
35+
$this->assertInstanceOf('\\Omnipay\\SpareChange\\BankGateway', $gateway);
36+
}
37+
38+
public function testCreateFullyQualified()
39+
{
40+
m::mock('alias:Omnipay\\Tests\\FooGateway');
41+
42+
$gateway = GatewayFactory::create('\\Omnipay\\Tests\\FooGateway');
43+
$this->assertInstanceOf('\\Omnipay\\Tests\\FooGateway', $gateway);
1344
}
1445

1546
/**
@@ -20,11 +51,4 @@ public function testCreateInvalid()
2051
{
2152
$gateway = GatewayFactory::create('Invalid');
2253
}
23-
24-
public function testFind()
25-
{
26-
$gateways = GatewayFactory::find();
27-
$this->assertContains('PayPal_Express', $gateways);
28-
$this->assertContains('Stripe', $gateways);
29-
}
3054
}

0 commit comments

Comments
 (0)