Completed
Push — master ( 60aec0...f632bc )
by Andreas
12:47 queued 10:41
created

GatewayFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
ccs 3
cts 5
cp 0.6
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 7 2
A create() 0 16 3
1
<?php
2
3
namespace Larium\Pay;
4
5
class GatewayFactory
6
{
7
    const GATEWAY_NAMESPACE = 'Larium\\Pay\\Gateway\\';
8
9
    private static $gateways = [];
10
11
    private function __construct()
12
    {
13
    }
14
15
    /**
16
     * Register a gateway with given name and class name.
17
     *
18
     * @throws Larium\Pay\GatewayException
19
     *
20
     * @param string $name The unique name for the gateway.
21
     * @param string $className The full class name of gateway.
22
     * @return void
23
     */
24
    public static function register($name, $className)
25
    {
26
        if (array_key_exists($name, self::$gateways)) {
27
            throw GatewayException::gatewayAlreadyRegistered($name);
28
        }
29
        self::$gateways[$name] = $className;
30
    }
31
32
    /**
33
     * Return an instance of a Gateway based on given name.
34
     *
35
     * @param string $name The registered name of Gateway
36
     * @param array $options Options for gateway
37
     * @return Larium\Pay\Gateway\Gateway
38
     */
39 2
    public static function create($name, array $options = [])
40
    {
41 2
        $gateway = self::GATEWAY_NAMESPACE . $name;
42
43
        if (class_exists($gateway)) {
44
            return new $gateway($options);
45
        }
46
47
        if (array_key_exists($name, self::$gateways)) {
48
            $gateway = self::$gateways[$name];
49
50
            return new $gateway($options);
51
        }
52
53
        throw GatewayException::invalidGatewayName($name);
54 2
    }
55
}
56