Completed
Push — master ( 7c6435...9f772a )
by Gaël
04:00
created

OmnipayGatewayManager::makeGateway()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace DansMaCulotte\Omnipay;
4
5
use Omnipay\Common\GatewayFactory;
6
7
class OmnipayGatewayManager
8
{
9
10
    /**
11
     * The application instance.
12
     *
13
     * @var \Illuminate\Foundation\Application
14
     */
15
    protected $app;
16
17
    /**
18
     * @var GatewayFactory
19
     */
20
    protected $factory;
21
22
    /**
23
     * The default settings, applied to every gateway
24
     */
25
    protected $defaults;
26
27
    /**
28
     * The registered gateways
29
     */
30
    protected $gateways;
31
32
    /**
33
     * The default gateway override
34
     */
35
    protected $gateway;
36
37
    /**
38
     * Create a new Gateway manager instance.
39
     *
40
     * @param  \Illuminate\Foundation\Application $app
41
     * @param  \Omnipay\Common\GatewayFactory $factory
42
     * @param  array
43
     */
44
    public function __construct($app, GatewayFactory $factory, $defaults = [])
45
    {
46
        $this->app = $app;
47
        $this->factory = $factory;
48
        $this->defaults = $defaults;
49
        $this->gateway = null;
50
    }
51
52
    /**
53
     * Get a gateway
54
     *
55
     * @return \Omnipay\Common\GatewayInterface
56
     */
57
    public function makeGateway()
58
    {
59
        $class = $this->gateway ?: $this->getDefaultGateway();
60
61
        if (!isset($this->gateways[$class])) {
62
            $gateway = $this->factory->create($class, null, $this->app['request']);
63
            $gateway->initialize($this->getConfig($class));
64
            $this->gateways[$class] = $gateway;
65
        }
66
67
        return $this->gateways[$class];
68
    }
69
70
    /**
71
     * Get the configuration, based on the config and the defaults.
72
     * @param string $name
73
     * @return array
74
     */
75
    protected function getConfig(string $name)
76
    {
77
        return array_merge(
78
            $this->defaults,
79
            $this->app['config']->get('omnipay.gateways.'.$name, [])
80
        );
81
    }
82
83
    /**
84
     * Get the default gateway name.
85
     *
86
     * @return string
87
     */
88
    public function getDefaultGateway()
89
    {
90
        return $this->app['config']['omnipay.default'];
91
    }
92
93
    /**
94
     * Set the default gateway name.
95
     *
96
     * @param string $name
97
     * @return void
98
     */
99
    public function setDefaultGateway(string $name)
100
    {
101
        $this->app['config']['omnipay.default'] = $name;
102
    }
103
104
    /**
105
     * @param string $name
106
     * @return OmnipayGatewayManager
107
     */
108
    public function withGateway(string $name)
109
    {
110
        $this->gateway = $name;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Dynamically call the default driver instance.
117
     *
118
     * @param  string  $method
119
     * @param  array   $parameters
120
     * @return mixed
121
     */
122
    public function __call($method, $parameters)
123
    {
124
        return call_user_func_array([$this->makeGateway(), $method], $parameters);
125
    }
126
}
127