vendor/payum/core/Payum/Core/Registry/DynamicRegistry.php line 72

Open in your IDE?
  1. <?php
  2. namespace Payum\Core\Registry;
  3. use Payum\Core\Exception\InvalidArgumentException;
  4. use Payum\Core\GatewayInterface;
  5. use Payum\Core\Model\GatewayConfigInterface;
  6. use Payum\Core\Storage\StorageInterface;
  7. class DynamicRegistry implements RegistryInterface
  8. {
  9.     /**
  10.      * @var GatewayInterface[]
  11.      */
  12.     private $gateways = [];
  13.     /**
  14.      * @var StorageInterface
  15.      */
  16.     private $gatewayConfigStore;
  17.     /**
  18.      * @var GatewayFactoryRegistryInterface|null
  19.      */
  20.     private $gatewayFactoryRegistry;
  21.     /**
  22.      * @deprecated since 1.3.3 will be removed in 2.0
  23.      *
  24.      * @var bool
  25.      */
  26.     private $backwardCompatibility = true;
  27.     /**
  28.      * @param StorageInterface $gatewayConfigStore
  29.      * @param GatewayFactoryRegistryInterface $gatewayFactoryRegistry
  30.      */
  31.     public function __construct(StorageInterface $gatewayConfigStore, GatewayFactoryRegistryInterface $gatewayFactoryRegistry)
  32.     {
  33.         $this->gatewayConfigStore = $gatewayConfigStore;
  34.         $this->gatewayFactoryRegistry = $gatewayFactoryRegistry;
  35.     }
  36.     /**
  37.      * {@inheritDoc}
  38.      */
  39.     public function getGatewayFactory($name)
  40.     {
  41.         // @deprecated It will throw invalid argument exception in 2.x
  42.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof  RegistryInterface) {
  43.             return $this->gatewayFactoryRegistry->getGatewayFactory($name);
  44.         }
  45.         throw new InvalidArgumentException(sprintf('Gateway factory "%s" does not exist.', $name));
  46.     }
  47.     /**
  48.      * {@inheritDoc}
  49.      */
  50.     public function getGatewayFactories()
  51.     {
  52.         // @deprecated It will return empty array here
  53.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof  RegistryInterface) {
  54.             return $this->gatewayFactoryRegistry->getGatewayFactories();
  55.         }
  56.         return [];
  57.     }
  58.     /**
  59.      * {@inheritDoc}
  60.      */
  61.     public function getGateway($name)
  62.     {
  63.         if (array_key_exists($name, $this->gateways)) {
  64.             return $this->gateways[$name];
  65.         }
  66.         if ($gatewayConfigs = $this->gatewayConfigStore->findBy(array('gatewayName' => $name))) {
  67.             $gateway = $this->createGateway(array_shift($gatewayConfigs));
  68.             $this->gateways[$name] = $gateway;
  69.             return $gateway;
  70.         }
  71.         // @deprecated It will throw invalid argument exception in 2.x
  72.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof RegistryInterface) {
  73.             return $this->gatewayFactoryRegistry->getGateway($name);
  74.         }
  75.         throw new InvalidArgumentException(sprintf('Gateway "%s" does not exist.', $name));
  76.     }
  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     public function getGateways()
  81.     {
  82.         // @deprecated It will return empty array here
  83.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof  RegistryInterface) {
  84.             return $this->gatewayFactoryRegistry->getGateways();
  85.         }
  86.         $gateways = [];
  87.         foreach ($this->gatewayConfigStore->findBy([]) as $gatewayConfig) {
  88.             /** @var GatewayConfigInterface $gatewayConfig */
  89.             $gateways[$gatewayConfig->getGatewayName()] = $this->getGateway($gatewayConfig->getGatewayName());
  90.         }
  91.         return $gateways;
  92.     }
  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     public function getStorage($class)
  97.     {
  98.         // @deprecated It will throw invalid argument exception in 2.x
  99.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof RegistryInterface) {
  100.             return $this->gatewayFactoryRegistry->getStorage($class);
  101.         }
  102.         throw new InvalidArgumentException(sprintf(
  103.             'Storage for given class "%s" does not exist.',
  104.             is_object($class) ? get_class($class) : $class
  105.         ));
  106.     }
  107.     /**
  108.      * {@inheritDoc}
  109.      */
  110.     public function getStorages()
  111.     {
  112.         // @deprecated It will return empty array here
  113.         if ($this->backwardCompatibility && $this->gatewayFactoryRegistry instanceof RegistryInterface) {
  114.             return $this->gatewayFactoryRegistry->getStorages();
  115.         }
  116.         return [];
  117.     }
  118.     /**
  119.      * @deprecated since 1.3.3 will be removed in 2.0
  120.      *
  121.      * @param boolean $backwardCompatibility
  122.      */
  123.     public function setBackwardCompatibility($backwardCompatibility)
  124.     {
  125.         $this->backwardCompatibility = $backwardCompatibility;
  126.     }
  127.     /**
  128.      * @param GatewayConfigInterface $gatewayConfig
  129.      *
  130.      * @return GatewayInterface
  131.      */
  132.     protected function createGateway(GatewayConfigInterface $gatewayConfig)
  133.     {
  134.         $config = $gatewayConfig->getConfig();
  135.         if (isset($config['factory'])) {
  136.             $factory = $this->gatewayFactoryRegistry->getGatewayFactory($config['factory']);
  137.             unset($config['factory']);
  138.         } else {
  139.             // BC
  140.             $factory = $this->gatewayFactoryRegistry->getGatewayFactory($gatewayConfig->getFactoryName());
  141.         }
  142.         return $factory->create($config);
  143.     }
  144. }