Passed
Push — master ( b8b2bd...fcde1f )
by Alex
02:15
created

GatewayManager::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nxmad\Larapay;
4
5
use RuntimeException;
6
use InvalidArgumentException;
7
use Nxmad\Larapay\Abstracts\Gateway;
8
use Illuminate\Contracts\Config\Repository;
9
10
class GatewayManager implements Contracts\Payments
11
{
12
    /**
13
     * The global GatewayManager config.
14
     *
15
     * @var Repository
16
     */
17
    protected $config;
18
19
    /**
20
     * Create a new manager instance.
21
     *
22
     * @param Repository $config
23
     */
24
    public function __construct(Repository $config)
25
    {
26
        $this->config = $config;
27
28
        foreach ($this->config->get('larapay.gateways') as $implementation => $config) {
29
            if (class_exists($implementation)) {
30
                $this->extend((new $implementation)->getSlug(), $implementation);
31
            }
32
        }
33
    }
34
35
    /**
36
     * The list of default implementations.
37
     *
38
     * @var array
39
     */
40
    protected $gateways = [];
41
42
    /**
43
     * The list of created drivers.
44
     *
45
     * @var array
46
     */
47
    protected $created = [];
48
49
    /**
50
     * Get gateway module instance.
51
     *
52
     * @param string $gateway
53
     *
54
     * @return Gateway
55
     */
56
    public function gateway(string $gateway): Gateway
57
    {
58
        if (! isset($this->created[$gateway])) {
59
            $this->created[$gateway] = $this->createGateway($gateway);
60
        }
61
62
        return $this->created[$gateway];
63
    }
64
65
    /**
66
     * Extend basic implementations list.
67
     *
68
     * @param string $driver
69
     * @param string $implementation
70
     *
71
     * @return self
72
     */
73
    public function extend(string $driver, string $implementation): self
74
    {
75
        $this->gateways[$driver] = $implementation;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Create and cache driver instance to fast access in future.
82
     *
83
     * @param string $gateway
84
     *
85
     * @return Gateway
86
     *
87
     * @throws InvalidArgumentException
88
     */
89
    protected function createGateway(string $gateway): Gateway
90
    {
91
        if (! isset($this->gateways[$gateway])) {
92
            throw new InvalidArgumentException("Gateway [{$gateway}] was not found.");
93
        }
94
95
        $implementation = $this->gateways[$gateway];
96
97
        if (! class_exists($implementation)) {
98
            throw new RuntimeException("Class [{$implementation}] was not found.");
99
        }
100
101
        if (! $this->config->has("larapay.gateways.{$implementation}")) {
102
            throw new RuntimeException("No config found for {$implementation}.");
103
        }
104
105
        $config = $this->config->get("larapay.gateways.{$implementation}");
106
107
        return new $implementation($config);
108
    }
109
}
110