GroupManagement.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Model;
  8. use Magento\Customer\Api\Data\GroupInterface;
  9. use Magento\Customer\Api\Data\GroupInterfaceFactory;
  10. use Magento\Customer\Api\GroupRepositoryInterface;
  11. use Magento\Framework\Api\FilterBuilder;
  12. use Magento\Framework\Api\SearchCriteriaBuilder;
  13. use Magento\Framework\Api\SortOrderBuilder;
  14. use Magento\Framework\App\Config\ScopeConfigInterface;
  15. use Magento\Framework\App\ObjectManager;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. use Magento\Store\Model\StoreManagerInterface;
  18. /**
  19. * The class contains methods for getting information about a customer group
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class GroupManagement implements \Magento\Customer\Api\GroupManagementInterface
  24. {
  25. const XML_PATH_DEFAULT_ID = 'customer/create_account/default_group';
  26. const NOT_LOGGED_IN_ID = 0;
  27. const CUST_GROUP_ALL = 32000;
  28. const GROUP_CODE_MAX_LENGTH = 32;
  29. /**
  30. * @var StoreManagerInterface
  31. */
  32. protected $storeManager;
  33. /**
  34. * @var ScopeConfigInterface
  35. */
  36. protected $scopeConfig;
  37. /**
  38. * @var GroupFactory
  39. */
  40. protected $groupFactory;
  41. /**
  42. * @var GroupRepositoryInterface
  43. */
  44. protected $groupRepository;
  45. /**
  46. * @var GroupInterfaceFactory
  47. */
  48. protected $groupDataFactory;
  49. /**
  50. * @var SearchCriteriaBuilder
  51. */
  52. protected $searchCriteriaBuilder;
  53. /**
  54. * @var FilterBuilder
  55. */
  56. protected $filterBuilder;
  57. /**
  58. * @var SortOrderBuilder
  59. */
  60. private $sortOrderBuilder;
  61. /**
  62. * @param StoreManagerInterface $storeManager
  63. * @param ScopeConfigInterface $scopeConfig
  64. * @param GroupFactory $groupFactory
  65. * @param GroupRepositoryInterface $groupRepository
  66. * @param GroupInterfaceFactory $groupDataFactory
  67. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  68. * @param FilterBuilder $filterBuilder
  69. * @param SortOrderBuilder $sortOrderBuilder
  70. */
  71. public function __construct(
  72. StoreManagerInterface $storeManager,
  73. ScopeConfigInterface $scopeConfig,
  74. GroupFactory $groupFactory,
  75. GroupRepositoryInterface $groupRepository,
  76. GroupInterfaceFactory $groupDataFactory,
  77. SearchCriteriaBuilder $searchCriteriaBuilder,
  78. FilterBuilder $filterBuilder,
  79. SortOrderBuilder $sortOrderBuilder = null
  80. ) {
  81. $this->storeManager = $storeManager;
  82. $this->scopeConfig = $scopeConfig;
  83. $this->groupFactory = $groupFactory;
  84. $this->groupRepository = $groupRepository;
  85. $this->groupDataFactory = $groupDataFactory;
  86. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  87. $this->filterBuilder = $filterBuilder;
  88. $this->sortOrderBuilder = $sortOrderBuilder ?: ObjectManager::getInstance()
  89. ->get(SortOrderBuilder::class);
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. public function isReadonly($groupId)
  95. {
  96. /** @var \Magento\Customer\Model\Group $group */
  97. $group = $this->groupFactory->create();
  98. $group->load($groupId);
  99. if ($group->getId() === null) {
  100. throw NoSuchEntityException::singleField('groupId', $groupId);
  101. }
  102. return $groupId == self::NOT_LOGGED_IN_ID || $group->usesAsDefault();
  103. }
  104. /**
  105. * @inheritdoc
  106. */
  107. public function getDefaultGroup($storeId = null)
  108. {
  109. if ($storeId === null) {
  110. $storeId = $this->storeManager->getStore()->getCode();
  111. }
  112. try {
  113. $groupId = $this->scopeConfig->getValue(
  114. self::XML_PATH_DEFAULT_ID,
  115. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  116. $storeId
  117. );
  118. } catch (\Magento\Framework\Exception\State\InitException $e) {
  119. throw NoSuchEntityException::singleField('storeId', $storeId);
  120. } catch (NoSuchEntityException $e) {
  121. throw NoSuchEntityException::singleField('storeId', $storeId);
  122. }
  123. try {
  124. return $this->groupRepository->getById($groupId);
  125. } catch (NoSuchEntityException $e) {
  126. throw NoSuchEntityException::doubleField('groupId', $groupId, 'storeId', $storeId);
  127. }
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function getNotLoggedInGroup()
  133. {
  134. return $this->groupRepository->getById(self::NOT_LOGGED_IN_ID);
  135. }
  136. /**
  137. * @inheritdoc
  138. */
  139. public function getLoggedInGroups()
  140. {
  141. $notLoggedInFilter[] = $this->filterBuilder
  142. ->setField(GroupInterface::ID)
  143. ->setConditionType('neq')
  144. ->setValue(self::NOT_LOGGED_IN_ID)
  145. ->create();
  146. $groupAll[] = $this->filterBuilder
  147. ->setField(GroupInterface::ID)
  148. ->setConditionType('neq')
  149. ->setValue(self::CUST_GROUP_ALL)
  150. ->create();
  151. $groupNameSortOrder = $this->sortOrderBuilder
  152. ->setField('customer_group_code')
  153. ->setAscendingDirection()
  154. ->create();
  155. $searchCriteria = $this->searchCriteriaBuilder
  156. ->addFilters($notLoggedInFilter)
  157. ->addFilters($groupAll)
  158. ->addSortOrder($groupNameSortOrder)
  159. ->create();
  160. return $this->groupRepository->getList($searchCriteria)->getItems();
  161. }
  162. /**
  163. * @inheritdoc
  164. */
  165. public function getAllCustomersGroup()
  166. {
  167. $groupDataObject = $this->groupDataFactory->create();
  168. $groupDataObject->setId(self::CUST_GROUP_ALL);
  169. return $groupDataObject;
  170. }
  171. }