Share.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Config;
  7. /**
  8. * Customer sharing config model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Share extends \Magento\Framework\App\Config\Value implements \Magento\Framework\Option\ArrayInterface
  13. {
  14. /**
  15. * Xml config path to customers sharing scope value
  16. *
  17. */
  18. const XML_PATH_CUSTOMER_ACCOUNT_SHARE = 'customer/account_share/scope';
  19. /**
  20. * Possible customer sharing scopes
  21. *
  22. */
  23. const SHARE_GLOBAL = 0;
  24. const SHARE_WEBSITE = 1;
  25. /**
  26. * @var \Magento\Customer\Model\ResourceModel\Customer
  27. */
  28. protected $_customerResource;
  29. /**
  30. * @var \Magento\Store\Model\StoreManagerInterface
  31. */
  32. protected $_storeManager;
  33. /**
  34. * Constructor
  35. *
  36. * @param \Magento\Framework\Model\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  39. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  40. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  41. * @param \Magento\Customer\Model\ResourceModel\Customer $customerResource
  42. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  43. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\Model\Context $context,
  48. \Magento\Framework\Registry $registry,
  49. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  50. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  51. \Magento\Store\Model\StoreManagerInterface $storeManager,
  52. \Magento\Customer\Model\ResourceModel\Customer $customerResource,
  53. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  54. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  55. array $data = []
  56. ) {
  57. $this->_storeManager = $storeManager;
  58. $this->_customerResource = $customerResource;
  59. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  60. }
  61. /**
  62. * Check whether current customers sharing scope is global
  63. *
  64. * @return bool
  65. */
  66. public function isGlobalScope()
  67. {
  68. return !$this->isWebsiteScope();
  69. }
  70. /**
  71. * Check whether current customers sharing scope is website
  72. *
  73. * @return bool
  74. */
  75. public function isWebsiteScope()
  76. {
  77. return $this->_config->getValue(
  78. self::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
  79. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  80. ) == self::SHARE_WEBSITE;
  81. }
  82. /**
  83. * Get possible sharing configuration options
  84. *
  85. * @return array
  86. */
  87. public function toOptionArray()
  88. {
  89. return [self::SHARE_GLOBAL => __('Global'), self::SHARE_WEBSITE => __('Per Website')];
  90. }
  91. /**
  92. * Check for email duplicates before saving customers sharing options
  93. *
  94. * @return $this
  95. * @throws \Magento\Framework\Exception\LocalizedException
  96. */
  97. public function beforeSave()
  98. {
  99. $value = $this->getValue();
  100. if ($value == self::SHARE_GLOBAL) {
  101. if ($this->_customerResource->findEmailDuplicates()) {
  102. //@codingStandardsIgnoreStart
  103. throw new \Magento\Framework\Exception\LocalizedException(
  104. __(
  105. 'We can\'t share customer accounts globally when the accounts share identical email addresses on more than one website.'
  106. )
  107. );
  108. //@codingStandardsIgnoreEnd
  109. }
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Returns shared website Ids.
  115. *
  116. * @param int $websiteId the ID to use if website scope is on
  117. * @return int[]
  118. */
  119. public function getSharedWebsiteIds($websiteId)
  120. {
  121. $ids = [];
  122. if ($this->isWebsiteScope()) {
  123. $ids[] = $websiteId;
  124. } else {
  125. foreach ($this->_storeManager->getWebsites() as $website) {
  126. $ids[] = $website->getId();
  127. }
  128. }
  129. return $ids;
  130. }
  131. }