Completed
Push — master ( c80d7c...01f162 )
by Colin
05:28
created

Omnipay::setDisabledGateways()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.0156
1
<?php
2
3
/*
4
 * This file is part of the colinodell\omnipay-bundle package.
5
 *
6
 * (c) 2018 Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ColinODell\OmnipayBundle\Service;
13
14
use Guzzle\Http\Client;
15
use Omnipay\Common\GatewayFactory;
16
use Omnipay\Common\GatewayInterface;
17
use Omnipay\Common\Helper;
18
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
19
20
class Omnipay
21
{
22
    /**
23
     * @var GatewayFactory
24
     */
25
    protected $gatewayFactory;
26
27
    /**
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * @var GatewayInterface[]
34
     */
35
    protected $cache;
36
37
    /**
38
     * @var GatewayInterface[]
39
     */
40
    protected $registeredGateways = [];
41
42
    /**
43
     * @var string[]
44
     */
45
    protected $disabledGateways = [];
46
47
    /**
48
     * @var string
49
     */
50
    protected $defaultGatewayName;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $initializeOnRegistration = false;
56
57
    /**
58
     * @param GatewayFactory $gatewayFactory
59
     * @param array          $config
60
     */
61 9
    public function __construct(GatewayFactory $gatewayFactory, array $config = array())
62
    {
63 9
        $this->gatewayFactory = $gatewayFactory;
64 9
        $this->config = $config;
65 9
    }
66
67
    /**
68
     * @param string $gatewayName
69
     *
70
     * @return GatewayInterface
71
     */
72 8
    public function get($gatewayName)
73
    {
74 8
        if (!isset($this->cache[$gatewayName])) {
75 8
            $gateway = $this->createGateway($gatewayName);
76 6
            $this->cache[$gatewayName] = $gateway;
77 6
        }
78
79 6
        return $this->cache[$gatewayName];
80
    }
81
82
    /**
83
     * @param GatewayInterface $gatewayInstance
84
     * @param string|null      $alias
85
     */
86 3
    public function registerGateway(GatewayInterface $gatewayInstance, $alias = null)
87
    {
88 3
        $name = $alias ?: Helper::getGatewayShortName(get_class($gatewayInstance));
89
90 3
        if (in_array($name, $this->disabledGateways)) {
91 1
            return;
92
        }
93
94 2
        $this->registeredGateways[$name] = $gatewayInstance;
95
96 2
        if ($this->initializeOnRegistration) {
97
            $gatewayInstance->initialize($this->getGatewayConfig($name));
98
            $this->cache[$name] = $gatewayInstance;
99
        }
100 2
    }
101
102
    /**
103
     * @param string[] $disabledGateways
104
     */
105 1
    public function setDisabledGateways(array $disabledGateways)
106
    {
107 1
        $this->disabledGateways = $disabledGateways;
108 1
    }
109
110
    /**
111
     * @return GatewayInterface
112
     */
113 2
    public function getDefaultGateway()
114
    {
115 2
        if (null === $this->defaultGatewayName) {
116 1
            throw new InvalidConfigurationException('Default gateway is not configured');
117
        }
118
119 1
        return $this->get($this->defaultGatewayName);
120
    }
121
122
    /**
123
     * @param string $defaultGatewayName
124
     */
125 1
    public function setDefaultGatewayName($defaultGatewayName)
126
    {
127 1
        $this->defaultGatewayName = $defaultGatewayName;
128 1
    }
129
130
    /**
131
     * @param boolean $initializeOnRegistration
132
     */
133
    public function initializeOnRegistration($initializeOnRegistration)
134
    {
135
        $this->initializeOnRegistration = $initializeOnRegistration;
136
    }
137
138
    /**
139
     * @param string $gatewayName
140
     * @return GatewayInterface
141
     */
142 8
    protected function createGateway($gatewayName)
143
    {
144 8
        $httpClient = new Client();
145
146 8
        if (isset($this->registeredGateways[$gatewayName])) {
147 2
            $gateway = $this->registeredGateways[$gatewayName];
148 2
        } else {
149
            /** @var GatewayInterface $gateway */
150 6
            $gateway = $this->gatewayFactory->create($gatewayName, $httpClient);
151
        }
152
153 6
        $gateway->initialize($this->getGatewayConfig($gatewayName));
154
155 6
        return $gateway;
156
    }
157
158
    /**
159
     * @param string $gatewayName
160
     * @return array
161
     */
162 6
    protected function getGatewayConfig($gatewayName)
163
    {
164 6
        return isset($this->config[$gatewayName]) ? $this->config[$gatewayName] : [];
165
    }
166
}
167