1 | <?php |
||
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 | public function __construct(GatewayFactory $gatewayFactory, array $config = array()) |
||
62 | { |
||
63 | $this->gatewayFactory = $gatewayFactory; |
||
64 | $this->config = $config; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string $gatewayName |
||
69 | * |
||
70 | * @return GatewayInterface |
||
71 | */ |
||
72 | public function get($gatewayName) |
||
73 | { |
||
74 | if (!isset($this->cache[$gatewayName])) { |
||
75 | $gateway = $this->createGateway($gatewayName); |
||
76 | $this->cache[$gatewayName] = $gateway; |
||
77 | } |
||
78 | |||
79 | return $this->cache[$gatewayName]; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param GatewayInterface $gatewayInstance |
||
84 | * @param string|null $alias |
||
85 | */ |
||
86 | public function registerGateway(GatewayInterface $gatewayInstance, $alias = null) |
||
101 | |||
102 | /** |
||
103 | * @param string[] $disabledGateways |
||
104 | */ |
||
105 | public function setDisabledGateways(array $disabledGateways) |
||
106 | { |
||
107 | $this->disabledGateways = $disabledGateways; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return GatewayInterface |
||
112 | */ |
||
113 | public function getDefaultGateway() |
||
114 | { |
||
115 | if (null === $this->defaultGatewayName) { |
||
116 | throw new InvalidConfigurationException('Default gateway is not configured'); |
||
117 | } |
||
118 | |||
119 | return $this->get($this->defaultGatewayName); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $defaultGatewayName |
||
124 | */ |
||
125 | public function setDefaultGatewayName($defaultGatewayName) |
||
126 | { |
||
127 | $this->defaultGatewayName = $defaultGatewayName; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param boolean $initializeOnRegistration |
||
132 | */ |
||
133 | public function initializeOnRegistration($initializeOnRegistration) |
||
137 | |||
138 | /** |
||
139 | * @param string $gatewayName |
||
140 | * @return GatewayInterface |
||
141 | */ |
||
142 | protected function createGateway($gatewayName) |
||
143 | { |
||
144 | $httpClient = new Client(); |
||
145 | |||
146 | if (isset($this->registeredGateways[$gatewayName])) { |
||
147 | $gateway = $this->registeredGateways[$gatewayName]; |
||
148 | } else { |
||
149 | /** @var GatewayInterface $gateway */ |
||
150 | $gateway = $this->gatewayFactory->create($gatewayName, $httpClient); |
||
151 | } |
||
152 | |||
153 | $gateway->initialize($this->getGatewayConfig($gatewayName)); |
||
154 | |||
155 | return $gateway; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param string $gatewayName |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function getGatewayConfig($gatewayName) |
||
166 | } |
||
167 |