GatewayFactory::all()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Omnipay Gateway Factory class
4
 */
5
6
namespace Omnipay\Common;
7
8
use Omnipay\Common\Exception\RuntimeException;
9
use Omnipay\Common\Http\ClientInterface;
10
use Symfony\Component\HttpFoundation\Request as HttpRequest;
11
12
/**
13
 * Omnipay Gateway Factory class
14
 *
15
 * This class abstracts a set of gateways that can be independently
16
 * registered, accessed, and used.
17
 *
18
 * Note that static calls to the Omnipay class are routed to this class by
19
 * the static call router (__callStatic) in Omnipay.
20
 *
21
 * Example:
22
 *
23
 * <code>
24
 *   // Create a gateway for the PayPal ExpressGateway
25
 *   // (routes to GatewayFactory::create)
26
 *   $gateway = Omnipay::create('ExpressGateway');
27
 * </code>
28
 *
29
 */
30
class GatewayFactory
31
{
32
    /**
33
     * Internal storage for all available gateways
34
     *
35
     * @var array
36
     */
37
    private $gateways = array();
38
39
    /**
40
     * All available gateways
41
     *
42
     * @return array An array of gateway names
43
     */
44 9
    public function all()
45
    {
46 9
        return $this->gateways;
47
    }
48
49
    /**
50
     * Replace the list of available gateways
51
     *
52
     * @param array $gateways An array of gateway names
53
     */
54 3
    public function replace(array $gateways)
55
    {
56 3
        $this->gateways = $gateways;
57 3
    }
58
59
    /**
60
     * Register a new gateway
61
     *
62
     * @param string $className Gateway name
63
     */
64 6
    public function register($className)
65
    {
66 6
        if (!in_array($className, $this->gateways)) {
67 6
            $this->gateways[] = $className;
68
        }
69 6
    }
70
71
    /**
72
     * Create a new gateway instance
73
     *
74
     * @param string               $class       Gateway name
75
     * @param ClientInterface|null $httpClient  A HTTP Client implementation
76
     * @param HttpRequest|null     $httpRequest A Symfony HTTP Request implementation
77
     * @throws RuntimeException                 If no such gateway is found
78
     * @return GatewayInterface                 An object of class $class is created and returned
79
     */
80 9
    public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
81
    {
82 9
        $class = Helper::getGatewayClassName($class);
83
84 9
        if (!class_exists($class)) {
85 3
            throw new RuntimeException("Class '$class' not found");
86
        }
87
88 6
        return new $class($httpClient, $httpRequest);
89
    }
90
}
91