Skip to content

Commit 3a4028e

Browse files
committed
Move static find() and create() methods to Omnipay class
1 parent 6332dba commit 3a4028e

File tree

5 files changed

+221
-25
lines changed

5 files changed

+221
-25
lines changed

composer.json

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
}
2424
],
2525
"autoload": {
26-
"psr-0": { "Omnipay\\Common\\" : "src/" }
26+
"psr-0": { "Omnipay\\Common\\" : "src/" },
27+
"classmap": ["src/Omnipay/Omnipay.php"]
2728
},
2829
"require": {
2930
"php": ">=5.3.2",
@@ -40,7 +41,40 @@
4041
"extra": {
4142
"branch-alias": {
4243
"dev-master": "2.0.x-dev"
43-
}
44+
},
45+
"gateways": [
46+
"AuthorizeNet_AIM",
47+
"AuthorizeNet_SIM",
48+
"Buckaroo",
49+
"CardSave",
50+
"Dummy",
51+
"Eway_Rapid",
52+
"FirstData_Connect",
53+
"GoCardless",
54+
"Manual",
55+
"Migs_ThreeParty",
56+
"Migs_TwoParty",
57+
"Mollie",
58+
"MultiSafepay",
59+
"Netaxept",
60+
"NetBanx",
61+
"PayFast",
62+
"Payflow_Pro",
63+
"PaymentExpress_PxPay",
64+
"PaymentExpress_PxPost",
65+
"PayPal_Express",
66+
"PayPal_Pro",
67+
"Pin",
68+
"SagePay_Direct",
69+
"SagePay_Server",
70+
"SecurePay_DirectPost",
71+
"Stripe",
72+
"TargetPay_Directebanking",
73+
"TargetPay_Ideal",
74+
"TargetPay_Mrcash",
75+
"TwoCheckout",
76+
"WorldPay"
77+
]
4478
},
4579
"minimum-stability": "dev",
4680
"prefer-stable": true

src/Omnipay/Common/GatewayFactory.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,57 @@
88

99
class GatewayFactory
1010
{
11-
private static $gateways = array();
11+
private $gateways = array();
1212

1313
/**
1414
* All available gateways
1515
*
1616
* @return array An array of gateway names
1717
*/
18-
public static function all()
18+
public function all()
1919
{
20-
return static::$gateways;
20+
return $this->gateways;
2121
}
2222

2323
/**
2424
* Replace the list of available gateways
2525
*
2626
* @param array $gateways An array of gateway names
2727
*/
28-
public static function replace(array $gateways)
28+
public function replace(array $gateways)
2929
{
30-
static::$gateways = $gateways;
30+
$this->gateways = $gateways;
3131
}
3232

3333
/**
3434
* Register a new gateway
3535
*
3636
* @param string $className Gateway name
3737
*/
38-
public static function register($className)
38+
public function register($className)
3939
{
40-
static::$gateways[] = $className;
40+
if (!in_array($className, $this->gateways)) {
41+
$this->gateways[] = $className;
42+
}
43+
}
44+
45+
/**
46+
* Automatically find and register all officially supported gateways
47+
*
48+
* @return array An array of gateway names
49+
*/
50+
public function find()
51+
{
52+
foreach ($this->getSupportedGateways() as $gateway) {
53+
$class = Helper::getGatewayClassName($gateway);
54+
if (class_exists($class)) {
55+
$this->register($gateway);
56+
}
57+
}
58+
59+
ksort($this->gateways);
60+
61+
return $this->all();
4162
}
4263

4364
/**
@@ -47,7 +68,7 @@ public static function register($className)
4768
* @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation
4869
* @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation
4970
*/
50-
public static function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
71+
public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
5172
{
5273
$class = Helper::getGatewayClassName($class);
5374

@@ -57,4 +78,16 @@ public static function create($class, ClientInterface $httpClient = null, HttpRe
5778

5879
return new $class($httpClient, $httpRequest);
5980
}
81+
82+
/**
83+
* Get a list of supported gateways which may be available
84+
*
85+
* @return array
86+
*/
87+
public function getSupportedGateways()
88+
{
89+
$package = json_decode(file_get_contents(__DIR__.'/../../../composer.json'), true);
90+
91+
return $package['extra']['gateways'];
92+
}
6093
}

src/Omnipay/Omnipay.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Omnipay;
4+
5+
use Omnipay\Common\GatewayFactory;
6+
7+
/**
8+
* Omnipay class
9+
*
10+
* Provides static access to the gateway factory methods
11+
*/
12+
class Omnipay
13+
{
14+
private static $factory;
15+
16+
/**
17+
* Get the gateway factory
18+
*
19+
* @return GatewayFactory A GatewayFactory instance
20+
*/
21+
public static function getFactory()
22+
{
23+
if (is_null(static::$factory)) {
24+
static::$factory = new GatewayFactory;
25+
}
26+
27+
return static::$factory;
28+
}
29+
30+
/**
31+
* Set the gateway factory
32+
*
33+
* @param GatewayFactory $factory A GatewayFactory instance
34+
*/
35+
public static function setFactory(GatewayFactory $factory = null)
36+
{
37+
static::$factory = $factory;
38+
}
39+
40+
public static function __callStatic($method, $parameters)
41+
{
42+
$factory = static::getFactory();
43+
44+
return call_user_func_array(array($factory, $method), $parameters);
45+
}
46+
}

tests/Omnipay/Common/GatewayFactoryTest.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,74 @@
77

88
class GatewayFactoryTest extends TestCase
99
{
10-
public function tearDown()
10+
public static function setUpBeforeClass()
1111
{
12-
GatewayFactory::replace(array());
12+
m::mock('alias:Omnipay\\SpareChange\\TestGateway');
13+
}
14+
15+
public function setUp()
16+
{
17+
$this->factory = new GatewayFactory;
1318
}
1419

1520
public function testReplace()
1621
{
1722
$gateways = array('Foo');
18-
GatewayFactory::replace($gateways);
23+
$this->factory->replace($gateways);
1924

20-
$this->assertSame($gateways, GatewayFactory::all());
25+
$this->assertSame($gateways, $this->factory->all());
2126
}
2227

2328
public function testRegister()
2429
{
25-
GatewayFactory::register('Bar');
30+
$this->factory->register('Bar');
2631

27-
$this->assertSame(array('Bar'), GatewayFactory::all());
32+
$this->assertSame(array('Bar'), $this->factory->all());
2833
}
2934

30-
public function testCreateShortName()
35+
public function testRegisterExistingGateway()
3136
{
32-
m::mock('alias:Omnipay\\SpareChange\\BankGateway');
37+
$this->factory->register('Milky');
38+
$this->factory->register('Bar');
39+
$this->factory->register('Bar');
3340

34-
$gateway = GatewayFactory::create('SpareChange_Bank');
35-
$this->assertInstanceOf('\\Omnipay\\SpareChange\\BankGateway', $gateway);
41+
$this->assertSame(array('Milky', 'Bar'), $this->factory->all());
3642
}
3743

38-
public function testCreateFullyQualified()
44+
public function testFindRegistersAvailableGateways()
45+
{
46+
$this->factory = m::mock('Omnipay\Common\GatewayFactory[getSupportedGateways]');
47+
$this->factory->shouldReceive('getSupportedGateways')->once()
48+
->andReturn(array('SpareChange_Test'));
49+
50+
$gateways = $this->factory->find();
51+
52+
$this->assertContains('SpareChange_Test', $gateways);
53+
$this->assertContains('SpareChange_Test', $this->factory->all());
54+
}
55+
56+
public function testFindIgnoresUnavailableGateways()
3957
{
40-
m::mock('alias:Omnipay\\Tests\\FooGateway');
58+
$this->factory = m::mock('Omnipay\Common\GatewayFactory[getSupportedGateways]');
59+
$this->factory->shouldReceive('getSupportedGateways')->once()
60+
->andReturn(array('SpareChange_Gone'));
61+
62+
$gateways = $this->factory->find();
4163

42-
$gateway = GatewayFactory::create('\\Omnipay\\Tests\\FooGateway');
43-
$this->assertInstanceOf('\\Omnipay\\Tests\\FooGateway', $gateway);
64+
$this->assertEmpty($gateways);
65+
$this->assertEmpty($this->factory->all());
66+
}
67+
68+
public function testCreateShortName()
69+
{
70+
$gateway = $this->factory->create('SpareChange_Test');
71+
$this->assertInstanceOf('\\Omnipay\\SpareChange\\TestGateway', $gateway);
72+
}
73+
74+
public function testCreateFullyQualified()
75+
{
76+
$gateway = $this->factory->create('\\Omnipay\\SpareChange\\TestGateway');
77+
$this->assertInstanceOf('\\Omnipay\\SpareChange\\TestGateway', $gateway);
4478
}
4579

4680
/**
@@ -49,6 +83,13 @@ public function testCreateFullyQualified()
4983
*/
5084
public function testCreateInvalid()
5185
{
52-
$gateway = GatewayFactory::create('Invalid');
86+
$gateway = $this->factory->create('Invalid');
87+
}
88+
89+
public function testGetSupportedGateways()
90+
{
91+
$gateways = $this->factory->getSupportedGateways();
92+
93+
$this->assertContains('Stripe', $gateways);
5394
}
5495
}

tests/Omnipay/OmnipayTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Omnipay;
4+
5+
use Mockery as m;
6+
use Omnipay\Tests\TestCase;
7+
8+
class OmnipayTest extends TestCase
9+
{
10+
public function tearDown()
11+
{
12+
Omnipay::setFactory(null);
13+
}
14+
15+
public function testGetFactory()
16+
{
17+
Omnipay::setFactory(null);
18+
19+
$factory = Omnipay::getFactory();
20+
$this->assertInstanceOf('Omnipay\Common\GatewayFactory', $factory);
21+
}
22+
23+
public function testSetFactory()
24+
{
25+
$factory = m::mock('Omnipay\Common\GatewayFactory');
26+
27+
Omnipay::setFactory($factory);
28+
29+
$this->assertSame($factory, Omnipay::getFactory());
30+
}
31+
32+
public function testCallStatic()
33+
{
34+
$factory = m::mock('Omnipay\Common\GatewayFactory');
35+
$factory->shouldReceive('testMethod')->with('some-argument')->once()->andReturn('some-result');
36+
37+
Omnipay::setFactory($factory);
38+
39+
$result = Omnipay::testMethod('some-argument');
40+
$this->assertSame('some-result', $result);
41+
}
42+
}

0 commit comments

Comments
 (0)