Config.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\App\Config\ScopeConfigInterface;
  8. use Magento\Payment\Gateway\ConfigInterface;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Default implementation of Payment Gateway Config.
  12. *
  13. * To process value retrieved from config implementation of \Magento\Payment\Gateway\Config\ValueHandlerInterface
  14. * should be used (inheritance and overriding of getValue method or introduction of new public methods
  15. * is not recommended).
  16. */
  17. class Config implements ConfigInterface
  18. {
  19. const DEFAULT_PATH_PATTERN = 'payment/%s/%s';
  20. /**
  21. * @var ScopeConfigInterface
  22. */
  23. private $scopeConfig;
  24. /**
  25. * @var string|null
  26. */
  27. private $methodCode;
  28. /**
  29. * @var string|null
  30. */
  31. private $pathPattern;
  32. /**
  33. * @param ScopeConfigInterface $scopeConfig
  34. * @param string|null $methodCode
  35. * @param string $pathPattern
  36. */
  37. public function __construct(
  38. ScopeConfigInterface $scopeConfig,
  39. $methodCode = null,
  40. $pathPattern = self::DEFAULT_PATH_PATTERN
  41. ) {
  42. $this->scopeConfig = $scopeConfig;
  43. $this->methodCode = $methodCode;
  44. $this->pathPattern = $pathPattern;
  45. }
  46. /**
  47. * Sets method code
  48. *
  49. * @param string $methodCode
  50. * @return void
  51. */
  52. public function setMethodCode($methodCode)
  53. {
  54. $this->methodCode = $methodCode;
  55. }
  56. /**
  57. * Sets path pattern
  58. *
  59. * @param string $pathPattern
  60. * @return void
  61. */
  62. public function setPathPattern($pathPattern)
  63. {
  64. $this->pathPattern = $pathPattern;
  65. }
  66. /**
  67. * Retrieve information from payment configuration
  68. *
  69. * @param string $field
  70. * @param int|null $storeId
  71. *
  72. * @return mixed
  73. */
  74. public function getValue($field, $storeId = null)
  75. {
  76. if ($this->methodCode === null || $this->pathPattern === null) {
  77. return null;
  78. }
  79. return $this->scopeConfig->getValue(
  80. sprintf($this->pathPattern, $this->methodCode, $field),
  81. ScopeInterface::SCOPE_STORE,
  82. $storeId
  83. );
  84. }
  85. }