GatewayFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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