|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Janwebdev\OmnipayBundle\Manager; |
| 4 | + |
| 5 | +use Omnipay\Common\Http\Client; |
| 6 | +use Omnipay\Common\GatewayFactory; |
| 7 | +use Omnipay\Common\GatewayInterface; |
| 8 | +use Omnipay\Common\Helper; |
| 9 | +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
| 10 | + |
| 11 | +class OmnipayManager |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var GatewayFactory |
| 15 | + */ |
| 16 | + protected GatewayFactory $gatewayFactory; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + protected array $config; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var GatewayInterface[] |
| 25 | + */ |
| 26 | + protected array $storage; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var GatewayInterface[] |
| 30 | + */ |
| 31 | + protected array $registeredGateways = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string[] |
| 35 | + */ |
| 36 | + protected array $disabledGateways = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string|null |
| 40 | + */ |
| 41 | + protected ?string $defaultGatewayName = null; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var bool |
| 45 | + */ |
| 46 | + protected bool $initOnBoot = false; |
| 47 | + |
| 48 | + public function __construct(GatewayFactory $gatewayFactory, array $config = []) |
| 49 | + { |
| 50 | + $this->gatewayFactory = $gatewayFactory; |
| 51 | + $this->config = $config; |
| 52 | + } |
| 53 | + |
| 54 | + public function get(string $gatewayName): GatewayInterface |
| 55 | + { |
| 56 | + if (!isset($this->storage[$gatewayName])) { |
| 57 | + $gateway = $this->createGateway($gatewayName); |
| 58 | + $this->storage[$gatewayName] = $gateway; |
| 59 | + } |
| 60 | + |
| 61 | + return $this->storage[$gatewayName]; |
| 62 | + } |
| 63 | + |
| 64 | + protected function createGateway(string $gatewayName): GatewayInterface |
| 65 | + { |
| 66 | + if (isset($this->registeredGateways[$gatewayName])) { |
| 67 | + $gateway = $this->registeredGateways[$gatewayName]; |
| 68 | + } else { |
| 69 | + $gateway = $this->gatewayFactory->create($gatewayName, new Client()); |
| 70 | + } |
| 71 | + |
| 72 | + return $gateway->initialize($this->getGatewayConfig($gatewayName)); |
| 73 | + } |
| 74 | + |
| 75 | + protected function getGatewayConfig(string $gatewayName): array |
| 76 | + { |
| 77 | + return $this->config[$gatewayName] ?? []; |
| 78 | + } |
| 79 | + |
| 80 | + public function registerGateway(GatewayInterface $gatewayInstance, ?string $alias = null): void |
| 81 | + { |
| 82 | + $name = $alias ?? Helper::getGatewayClassName(get_class($gatewayInstance)); |
| 83 | + |
| 84 | + if (in_array($name, $this->disabledGateways, true)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $this->registeredGateways[$name] = $gatewayInstance; |
| 89 | + |
| 90 | + if ($this->initOnBoot) { |
| 91 | + $gatewayInstance->initialize($this->getGatewayConfig($name)); |
| 92 | + $this->storage[$name] = $gatewayInstance; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public function setDisabledGateways(array $disabledGateways): void |
| 97 | + { |
| 98 | + $this->disabledGateways = $disabledGateways; |
| 99 | + } |
| 100 | + |
| 101 | + public function getDefaultGateway(): GatewayInterface |
| 102 | + { |
| 103 | + if (null === $this->defaultGatewayName) { |
| 104 | + throw new InvalidConfigurationException('Default gateway is not configured'); |
| 105 | + } |
| 106 | + |
| 107 | + return $this->get($this->defaultGatewayName); |
| 108 | + } |
| 109 | + |
| 110 | + public function setDefaultGatewayName(string $defaultGatewayName): void |
| 111 | + { |
| 112 | + $this->defaultGatewayName = $defaultGatewayName; |
| 113 | + } |
| 114 | + |
| 115 | + public function initOnBoot(bool $initOnBoot): void |
| 116 | + { |
| 117 | + $this->initOnBoot = $initOnBoot; |
| 118 | + } |
| 119 | +} |
0 commit comments