Completed
Push — master ( 38f614...f1c5d8 )
by Gregorio
01:56
created

GatewayFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 52
ccs 15
cts 18
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 2
A resolveParameters() 0 16 4
A gatewayClass() 0 8 2
1
<?php
2
3
namespace Gregoriohc\Moneta\Common;
4
5
use RuntimeException;
6
7
class GatewayFactory
8
{
9
    /**
10
     * Create a new gateway instance
11
     *
12
     * @param string $name
13
     * @param array $parameters
14
     * @return GatewayInterface
15
     */
16 45
    public function create($name, $parameters = null)
17
    {
18 45
        $class = $this->gatewayClass($name);
19
20 45
        if (!class_exists($class)) {
21 3
            throw new RuntimeException("Gateway class '$class' does not exists");
22
        }
23
24 42
        $parameters = $this->resolveParameters($name, $parameters);
25
26 42
        return new $class($parameters);
27
    }
28
29 42
    private function resolveParameters($name, $parameters)
30
    {
31 42
        if (!is_array($parameters)) {
32 3
            $parameters = [];
33
        }
34
35 42
        if (function_exists('config')) {
36
            $parameters = config('moneta.gateways.' . $name, []);
37
38
            if (!array_key_exists('test_mode', $parameters)) {
39
                $parameters = config('moneta.test_mode', true);
40
            }
41
        }
42
43 42
        return $parameters;
44
    }
45
46
    /**
47
     * @param string $name
48
     * @return string
49
     */
50 45
    private function gatewayClass($name)
51
    {
52 45
        if (strstr($name, '\\')) {
53 42
            return $name;
54
        }
55
56 3
        return 'Gregoriohc\\Moneta\\' . ucfirst($name) . '\\Gateway';
57
    }
58
}