MerchantCountry.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\System\Config\Backend;
  7. /**
  8. * Backend model for merchant country. Default country used instead of empty value.
  9. */
  10. class MerchantCountry extends \Magento\Framework\App\Config\Value
  11. {
  12. /**
  13. * Core data
  14. *
  15. * @var \Magento\Directory\Helper\Data
  16. */
  17. protected $directoryHelper;
  18. /**
  19. * @var \Magento\Store\Model\StoreManagerInterface
  20. */
  21. protected $_storeManager;
  22. /**
  23. * @param \Magento\Framework\Model\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  26. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  27. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  28. * @param \Magento\Directory\Helper\Data $directoryHelper
  29. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  30. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  31. * @param array $data
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\Context $context,
  35. \Magento\Framework\Registry $registry,
  36. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  37. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  38. \Magento\Store\Model\StoreManagerInterface $storeManager,
  39. \Magento\Directory\Helper\Data $directoryHelper,
  40. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  41. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  42. array $data = []
  43. ) {
  44. $this->directoryHelper = $directoryHelper;
  45. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  46. $this->_storeManager = $storeManager;
  47. }
  48. /**
  49. * Substitute empty value with Default country.
  50. *
  51. * @return void
  52. */
  53. protected function _afterLoad()
  54. {
  55. $value = (string)$this->getValue();
  56. if (empty($value)) {
  57. if ($this->getWebsite()) {
  58. $defaultCountry = $this->_storeManager->getWebsite(
  59. $this->getWebsite()
  60. )->getConfig(
  61. \Magento\Directory\Helper\Data::XML_PATH_DEFAULT_COUNTRY
  62. );
  63. } else {
  64. $defaultCountry = $this->directoryHelper->getDefaultCountry($this->getStore());
  65. }
  66. $this->setValue($defaultCountry);
  67. }
  68. }
  69. }