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

GatewayFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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