ValueHandlerPool.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Config;
  7. use Magento\Framework\ObjectManager\TMap;
  8. use Magento\Framework\ObjectManager\TMapFactory;
  9. /**
  10. * Default implementation of config value handlers pool.
  11. * This class designed to be base for virtual types.
  12. * Direct injection of this class is not recommended (inject ValueHandlerPoolInterface instead).
  13. * Inheritance from this class is not recommended (declare virtual type or implement ValueHandlerPoolInterface instead).
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class ValueHandlerPool implements \Magento\Payment\Gateway\Config\ValueHandlerPoolInterface
  19. {
  20. /**
  21. * Default handler code
  22. */
  23. const DEFAULT_HANDLER = 'default';
  24. /**
  25. * @var ValueHandlerInterface[] | TMap
  26. */
  27. private $handlers;
  28. /**
  29. * @param TMapFactory $tmapFactory
  30. * @param array $handlers
  31. */
  32. public function __construct(
  33. TMapFactory $tmapFactory,
  34. array $handlers
  35. ) {
  36. if (!isset($handlers[self::DEFAULT_HANDLER])) {
  37. throw new \LogicException('Default handler should be provided.');
  38. }
  39. $this->handlers = $tmapFactory->create(
  40. [
  41. 'array' => $handlers,
  42. 'type' => ValueHandlerInterface::class
  43. ]
  44. );
  45. }
  46. /**
  47. * Retrieves an appropriate configuration value handler
  48. *
  49. * @param string $field
  50. * @return ValueHandlerInterface
  51. */
  52. public function get($field)
  53. {
  54. return isset($this->handlers[$field])
  55. ? $this->handlers[$field]
  56. : $this->handlers[self::DEFAULT_HANDLER];
  57. }
  58. }