Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupManagement.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Customer\Model;
9 
17 use Magento\Customer\Api\Data\GroupInterfaceFactory;
18 use Magento\Customer\Model\GroupFactory;
19 
24 {
25  const XML_PATH_DEFAULT_ID = 'customer/create_account/default_group';
26 
27  const NOT_LOGGED_IN_ID = 0;
28 
29  const CUST_GROUP_ALL = 32000;
30 
32 
36  protected $storeManager;
37 
41  protected $scopeConfig;
42 
46  protected $groupFactory;
47 
51  protected $groupRepository;
52 
56  protected $groupDataFactory;
57 
62 
66  protected $filterBuilder;
67 
77  public function __construct(
80  GroupFactory $groupFactory,
82  GroupInterfaceFactory $groupDataFactory,
85  ) {
86  $this->storeManager = $storeManager;
87  $this->scopeConfig = $scopeConfig;
88  $this->groupFactory = $groupFactory;
89  $this->groupRepository = $groupRepository;
90  $this->groupDataFactory = $groupDataFactory;
91  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
92  $this->filterBuilder = $filterBuilder;
93  }
94 
98  public function isReadonly($groupId)
99  {
101  $group = $this->groupFactory->create();
102  $group->load($groupId);
103  if ($group->getId() === null) {
105  }
106  return $groupId == self::NOT_LOGGED_IN_ID || $group->usesAsDefault();
107  }
108 
112  public function getDefaultGroup($storeId = null)
113  {
114  if ($storeId === null) {
115  $storeId = $this->storeManager->getStore()->getCode();
116  }
117  try {
118  $groupId = $this->scopeConfig->getValue(
119  self::XML_PATH_DEFAULT_ID,
120  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
121  $storeId
122  );
123  } catch (\Magento\Framework\Exception\State\InitException $e) {
125  } catch (NoSuchEntityException $e) {
127  }
128  try {
129  return $this->groupRepository->getById($groupId);
130  } catch (NoSuchEntityException $e) {
131  throw NoSuchEntityException::doubleField('groupId', $groupId, 'storeId', $storeId);
132  }
133  }
134 
138  public function getNotLoggedInGroup()
139  {
140  return $this->groupRepository->getById(self::NOT_LOGGED_IN_ID);
141  }
142 
146  public function getLoggedInGroups()
147  {
148  $notLoggedInFilter[] = $this->filterBuilder
149  ->setField(GroupInterface::ID)
150  ->setConditionType('neq')
151  ->setValue(self::NOT_LOGGED_IN_ID)
152  ->create();
153  $groupAll[] = $this->filterBuilder
154  ->setField(GroupInterface::ID)
155  ->setConditionType('neq')
156  ->setValue(self::CUST_GROUP_ALL)
157  ->create();
158  $searchCriteria = $this->searchCriteriaBuilder
159  ->addFilters($notLoggedInFilter)
160  ->addFilters($groupAll)
161  ->create();
162  return $this->groupRepository->getList($searchCriteria)->getItems();
163  }
164 
168  public function getAllCustomersGroup()
169  {
170  $groupDataObject = $this->groupDataFactory->create();
171  $groupDataObject->setId(self::CUST_GROUP_ALL);
172  return $groupDataObject;
173  }
174 }
static doubleField($fieldName, $fieldValue, $secondFieldName, $secondFieldValue)
$groupDataObject
$group
Definition: sections.phtml:16
$searchCriteria
__construct(StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, GroupFactory $groupFactory, GroupRepositoryInterface $groupRepository, GroupInterfaceFactory $groupDataFactory, SearchCriteriaBuilder $searchCriteriaBuilder, FilterBuilder $filterBuilder)