Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupRepository.php
Go to the documentation of this file.
1 <?php
8 
19 use Magento\Customer\Api\Data\GroupExtensionInterface;
20 
27 {
32 
36  protected $groupRegistry;
37 
41  protected $groupFactory;
42 
46  protected $groupDataFactory;
47 
52 
57 
62 
66  private $taxClassRepository;
67 
72 
76  private $collectionProcessor;
77 
89  public function __construct(
91  \Magento\Customer\Model\GroupFactory $groupFactory,
92  \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,
94  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
95  \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory $searchResultsFactory,
96  \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepositoryInterface,
97  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
98  CollectionProcessorInterface $collectionProcessor = null
99  ) {
100  $this->groupRegistry = $groupRegistry;
101  $this->groupFactory = $groupFactory;
102  $this->groupDataFactory = $groupDataFactory;
103  $this->groupResourceModel = $groupResourceModel;
104  $this->dataObjectProcessor = $dataObjectProcessor;
105  $this->searchResultsFactory = $searchResultsFactory;
106  $this->taxClassRepository = $taxClassRepositoryInterface;
107  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
108  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
109  }
110 
114  public function save(\Magento\Customer\Api\Data\GroupInterface $group)
115  {
116  $this->_validate($group);
117 
119  $groupModel = null;
120  if ($group->getId() || (string)$group->getId() === '0') {
121  $this->_verifyTaxClassModel($group->getTaxClassId(), $group);
122  $groupModel = $this->groupRegistry->retrieve($group->getId());
123  $groupDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
124  $group,
125  \Magento\Customer\Api\Data\GroupInterface::class
126  );
127  foreach ($groupDataAttributes as $attributeCode => $attributeData) {
128  $groupModel->setDataUsingMethod($attributeCode, $attributeData);
129  }
130  } else {
131  $groupModel = $this->groupFactory->create();
132  $groupModel->setCode($group->getCode());
133 
134  $taxClassId = $group->getTaxClassId() ?: self::DEFAULT_TAX_CLASS_ID;
135  $this->_verifyTaxClassModel($taxClassId, $group);
136  $groupModel->setTaxClassId($taxClassId);
137  }
138 
139  try {
140  $this->groupResourceModel->save($groupModel);
141  } catch (\Magento\Framework\Exception\LocalizedException $e) {
146  if ($e->getMessage() == (string)__('Customer Group already exists.')) {
147  throw new InvalidTransitionException(__('Customer Group already exists.'));
148  }
149  throw $e;
150  }
151 
152  $this->groupRegistry->remove($groupModel->getId());
153 
154  $groupDataObject = $this->groupDataFactory->create()
155  ->setId($groupModel->getId())
156  ->setCode($groupModel->getCode())
157  ->setTaxClassId($groupModel->getTaxClassId())
158  ->setTaxClassName($groupModel->getTaxClassName());
159 
160  if ($group->getExtensionAttributes()) {
161  $groupDataObject->setExtensionAttributes($group->getExtensionAttributes());
162  }
163 
164  return $groupDataObject;
165  }
166 
170  public function getById($id)
171  {
172  $groupModel = $this->groupRegistry->retrieve($id);
173  $groupDataObject = $this->groupDataFactory->create()
174  ->setId($groupModel->getId())
175  ->setCode($groupModel->getCode())
176  ->setTaxClassId($groupModel->getTaxClassId())
177  ->setTaxClassName($groupModel->getTaxClassName());
178  return $groupDataObject;
179  }
180 
185  {
186  $searchResults = $this->searchResultsFactory->create();
187  $searchResults->setSearchCriteria($searchCriteria);
188 
190  $collection = $this->groupFactory->create()->getCollection();
191  $groupInterfaceName = \Magento\Customer\Api\Data\GroupInterface::class;
192  $this->extensionAttributesJoinProcessor->process($collection, $groupInterfaceName);
193  $collection->addTaxClass();
194 
195  $this->collectionProcessor->process($searchCriteria, $collection);
196 
198  $groups = [];
200  foreach ($collection as $group) {
202  $groupDataObject = $this->groupDataFactory->create()
203  ->setId($group->getId())
204  ->setCode($group->getCode())
205  ->setTaxClassId($group->getTaxClassId())
206  ->setTaxClassName($group->getTaxClassName());
207  $data = $group->getData();
208  $data = $this->extensionAttributesJoinProcessor->extractExtensionAttributes($groupInterfaceName, $data);
210  && ($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY] instanceof GroupExtensionInterface)
211  ) {
213  }
214  $groups[] = $groupDataObject;
215  }
216  $searchResults->setTotalCount($collection->getSize());
217  return $searchResults->setItems($groups);
218  }
219 
230  {
231  $fields = [];
232  $conditions = [];
233  foreach ($filterGroup->getFilters() as $filter) {
234  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
235  $fields[] = $this->translateField($filter->getField());
236  $conditions[] = [$condition => $filter->getValue()];
237  }
238  if ($fields) {
239  $collection->addFieldToFilter($fields, $conditions);
240  }
241  }
242 
250  protected function translateField($field)
251  {
252  switch ($field) {
254  return 'customer_group_code';
255  case GroupInterface::ID:
256  return 'customer_group_id';
258  return 'class_name';
259  default:
260  return $field;
261  }
262  }
263 
272  public function delete(GroupInterface $group)
273  {
274  return $this->deleteById($group->getId());
275  }
276 
286  public function deleteById($id)
287  {
288  $groupModel = $this->groupRegistry->retrieve($id);
289 
290  if ($id <= 0 || $groupModel->usesAsDefault()) {
291  throw new \Magento\Framework\Exception\StateException(__('Cannot delete group.'));
292  }
293 
294  $groupModel->delete();
295  $this->groupRegistry->remove($id);
296  return true;
297  }
298 
309  private function _validate($group)
310  {
311  $exception = new InputException();
312  if (!\Zend_Validate::is($group->getCode(), 'NotEmpty')) {
313  $exception->addError(__('"%fieldName" is required. Enter and try again.', ['fieldName' => 'code']));
314  }
315 
316  if ($exception->wasErrorAdded()) {
317  throw $exception;
318  }
319  }
320 
329  protected function _verifyTaxClassModel($taxClassId, $group)
330  {
331  try {
332  /* @var TaxClassInterface $taxClassData */
333  $taxClassData = $this->taxClassRepository->get($taxClassId);
334  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
335  throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
336  }
337  if ($taxClassData->getClassType() !== TaxClassManagementInterface::TYPE_CUSTOMER) {
338  throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
339  }
340  }
341 
348  private function getCollectionProcessor()
349  {
350  if (!$this->collectionProcessor) {
351  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
352  'Magento\Customer\Model\Api\SearchCriteria\GroupCollectionProcessor'
353  );
354  }
355  return $this->collectionProcessor;
356  }
357 }
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$groupDataObject
$id
Definition: fieldset.phtml:14
static invalidFieldValue($fieldName, $fieldValue, \Exception $cause=null)
$group
Definition: sections.phtml:16
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
$searchCriteria
$attributeCode
Definition: extend.phtml:12
__construct(\Magento\Customer\Model\GroupRegistry $groupRegistry, \Magento\Customer\Model\GroupFactory $groupFactory, \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory, \Magento\Customer\Model\ResourceModel\Group $groupResourceModel, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory $searchResultsFactory, \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepositoryInterface, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor, CollectionProcessorInterface $collectionProcessor=null)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
save(\Magento\Customer\Api\Data\GroupInterface $group)