PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-customer/Model/ResourceModel/GroupRepository.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 348 lines | 197 code | 32 blank | 119 comment | 16 complexity | 3fdec78cb57f4daae6d62ba3ffaa3ee0 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel;
  7. use Magento\Customer\Api\Data\GroupInterface;
  8. use Magento\Customer\Model\ResourceModel\Group\Collection;
  9. use Magento\Framework\Api\Search\FilterGroup;
  10. use Magento\Framework\Api\SearchCriteriaInterface;
  11. use Magento\Framework\Api\SortOrder;
  12. use Magento\Framework\Exception\InputException;
  13. use Magento\Framework\Exception\State\InvalidTransitionException;
  14. use Magento\Tax\Api\Data\TaxClassInterface;
  15. use Magento\Tax\Api\TaxClassManagementInterface;
  16. use Magento\Framework\Api\ExtensibleDataInterface;
  17. use Magento\Customer\Api\Data\GroupExtensionInterface;
  18. /**
  19. * Customer group CRUD class
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class GroupRepository implements \Magento\Customer\Api\GroupRepositoryInterface
  24. {
  25. /**
  26. * The default tax class id if no tax class id is specified
  27. */
  28. const DEFAULT_TAX_CLASS_ID = 3;
  29. /**
  30. * @var \Magento\Customer\Model\GroupRegistry
  31. */
  32. protected $groupRegistry;
  33. /**
  34. * @var \Magento\Customer\Model\GroupFactory
  35. */
  36. protected $groupFactory;
  37. /**
  38. * @var \Magento\Customer\Api\Data\GroupInterfaceFactory
  39. */
  40. protected $groupDataFactory;
  41. /**
  42. * @var \Magento\Customer\Model\ResourceModel\Group
  43. */
  44. protected $groupResourceModel;
  45. /**
  46. * @var \Magento\Framework\Reflection\DataObjectProcessor
  47. */
  48. protected $dataObjectProcessor;
  49. /**
  50. * @var \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory
  51. */
  52. protected $searchResultsFactory;
  53. /**
  54. * @var \Magento\Tax\Api\TaxClassRepositoryInterface
  55. */
  56. private $taxClassRepository;
  57. /**
  58. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  59. */
  60. protected $extensionAttributesJoinProcessor;
  61. /**
  62. * @param \Magento\Customer\Model\GroupRegistry $groupRegistry
  63. * @param \Magento\Customer\Model\GroupFactory $groupFactory
  64. * @param \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory
  65. * @param \Magento\Customer\Model\ResourceModel\Group $groupResourceModel
  66. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  67. * @param \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory $searchResultsFactory
  68. * @param \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepositoryInterface
  69. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
  70. */
  71. public function __construct(
  72. \Magento\Customer\Model\GroupRegistry $groupRegistry,
  73. \Magento\Customer\Model\GroupFactory $groupFactory,
  74. \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,
  75. \Magento\Customer\Model\ResourceModel\Group $groupResourceModel,
  76. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  77. \Magento\Customer\Api\Data\GroupSearchResultsInterfaceFactory $searchResultsFactory,
  78. \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassRepositoryInterface,
  79. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
  80. ) {
  81. $this->groupRegistry = $groupRegistry;
  82. $this->groupFactory = $groupFactory;
  83. $this->groupDataFactory = $groupDataFactory;
  84. $this->groupResourceModel = $groupResourceModel;
  85. $this->dataObjectProcessor = $dataObjectProcessor;
  86. $this->searchResultsFactory = $searchResultsFactory;
  87. $this->taxClassRepository = $taxClassRepositoryInterface;
  88. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function save(\Magento\Customer\Api\Data\GroupInterface $group)
  94. {
  95. $this->_validate($group);
  96. /** @var \Magento\Customer\Model\Group $groupModel */
  97. $groupModel = null;
  98. if ($group->getId()) {
  99. $this->_verifyTaxClassModel($group->getTaxClassId(), $group);
  100. $groupModel = $this->groupRegistry->retrieve($group->getId());
  101. $groupDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
  102. $group,
  103. '\Magento\Customer\Api\Data\GroupInterface'
  104. );
  105. foreach ($groupDataAttributes as $attributeCode => $attributeData) {
  106. $groupModel->setDataUsingMethod($attributeCode, $attributeData);
  107. }
  108. } else {
  109. $groupModel = $this->groupFactory->create();
  110. $groupModel->setCode($group->getCode());
  111. $taxClassId = $group->getTaxClassId() ?: self::DEFAULT_TAX_CLASS_ID;
  112. $this->_verifyTaxClassModel($taxClassId, $group);
  113. $groupModel->setTaxClassId($taxClassId);
  114. }
  115. try {
  116. $this->groupResourceModel->save($groupModel);
  117. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  118. /**
  119. * Would like a better way to determine this error condition but
  120. * difficult to do without imposing more database calls
  121. */
  122. if ($e->getMessage() == (string)__('Customer Group already exists.')) {
  123. throw new InvalidTransitionException(__('Customer Group already exists.'));
  124. }
  125. throw $e;
  126. }
  127. $this->groupRegistry->remove($groupModel->getId());
  128. $groupDataObject = $this->groupDataFactory->create()
  129. ->setId($groupModel->getId())
  130. ->setCode($groupModel->getCode())
  131. ->setTaxClassId($groupModel->getTaxClassId())
  132. ->setTaxClassName($groupModel->getTaxClassName());
  133. return $groupDataObject;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getById($id)
  139. {
  140. $groupModel = $this->groupRegistry->retrieve($id);
  141. $groupDataObject = $this->groupDataFactory->create()
  142. ->setId($groupModel->getId())
  143. ->setCode($groupModel->getCode())
  144. ->setTaxClassId($groupModel->getTaxClassId())
  145. ->setTaxClassName($groupModel->getTaxClassName());
  146. return $groupDataObject;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function getList(SearchCriteriaInterface $searchCriteria)
  152. {
  153. $searchResults = $this->searchResultsFactory->create();
  154. $searchResults->setSearchCriteria($searchCriteria);
  155. /** @var \Magento\Customer\Model\ResourceModel\Group\Collection $collection */
  156. $collection = $this->groupFactory->create()->getCollection();
  157. $groupInterfaceName = 'Magento\Customer\Api\Data\GroupInterface';
  158. $this->extensionAttributesJoinProcessor->process($collection, $groupInterfaceName);
  159. $collection->addTaxClass();
  160. //Add filters from root filter group to the collection
  161. /** @var FilterGroup $group */
  162. foreach ($searchCriteria->getFilterGroups() as $group) {
  163. $this->addFilterGroupToCollection($group, $collection);
  164. }
  165. $sortOrders = $searchCriteria->getSortOrders();
  166. /** @var SortOrder $sortOrder */
  167. if ($sortOrders) {
  168. foreach ($searchCriteria->getSortOrders() as $sortOrder) {
  169. $field = $this->translateField($sortOrder->getField());
  170. $collection->addOrder(
  171. $field,
  172. ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
  173. );
  174. }
  175. } else {
  176. // set a default sorting order since this method is used constantly in many
  177. // different blocks
  178. $field = $this->translateField('id');
  179. $collection->addOrder($field, 'ASC');
  180. }
  181. $collection->setCurPage($searchCriteria->getCurrentPage());
  182. $collection->setPageSize($searchCriteria->getPageSize());
  183. /** @var \Magento\Customer\Api\Data\GroupInterface[] $groups */
  184. $groups = [];
  185. /** @var \Magento\Customer\Model\Group $group */
  186. foreach ($collection as $group) {
  187. /** @var \Magento\Customer\Api\Data\GroupInterface $groupDataObject */
  188. $groupDataObject = $this->groupDataFactory->create()
  189. ->setId($group->getId())
  190. ->setCode($group->getCode())
  191. ->setTaxClassId($group->getTaxClassId())
  192. ->setTaxClassName($group->getTaxClassName());
  193. $data = $group->getData();
  194. $data = $this->extensionAttributesJoinProcessor->extractExtensionAttributes($groupInterfaceName, $data);
  195. if (isset($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY])
  196. && ($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY] instanceof GroupExtensionInterface)
  197. ) {
  198. $groupDataObject->setExtensionAttributes($data[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
  199. }
  200. $groups[] = $groupDataObject;
  201. }
  202. $searchResults->setTotalCount($collection->getSize());
  203. return $searchResults->setItems($groups);
  204. }
  205. /**
  206. * Helper function that adds a FilterGroup to the collection.
  207. *
  208. * @param FilterGroup $filterGroup
  209. * @param Collection $collection
  210. * @return void
  211. * @throws \Magento\Framework\Exception\InputException
  212. */
  213. protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
  214. {
  215. $fields = [];
  216. $conditions = [];
  217. foreach ($filterGroup->getFilters() as $filter) {
  218. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  219. $fields[] = $this->translateField($filter->getField());
  220. $conditions[] = [$condition => $filter->getValue()];
  221. }
  222. if ($fields) {
  223. $collection->addFieldToFilter($fields, $conditions);
  224. }
  225. }
  226. /**
  227. * Translates a field name to a DB column name for use in collection queries.
  228. *
  229. * @param string $field a field name that should be translated to a DB column name.
  230. * @return string
  231. */
  232. protected function translateField($field)
  233. {
  234. switch ($field) {
  235. case GroupInterface::CODE:
  236. return 'customer_group_code';
  237. case GroupInterface::ID:
  238. return 'customer_group_id';
  239. case GroupInterface::TAX_CLASS_NAME:
  240. return 'class_name';
  241. default:
  242. return $field;
  243. }
  244. }
  245. /**
  246. * Delete customer group.
  247. *
  248. * @param GroupInterface $group
  249. * @return bool true on success
  250. * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
  251. * @throws \Magento\Framework\Exception\LocalizedException
  252. */
  253. public function delete(GroupInterface $group)
  254. {
  255. return $this->deleteById($group->getId());
  256. }
  257. /**
  258. * Delete customer group by ID.
  259. *
  260. * @param int $id
  261. * @return bool true on success
  262. * @throws \Magento\Framework\Exception\NoSuchEntityException
  263. * @throws \Magento\Framework\Exception\StateException If customer group cannot be deleted
  264. * @throws \Magento\Framework\Exception\LocalizedException
  265. */
  266. public function deleteById($id)
  267. {
  268. $groupModel = $this->groupRegistry->retrieve($id);
  269. if ($id <= 0 || $groupModel->usesAsDefault()) {
  270. throw new \Magento\Framework\Exception\StateException(__('Cannot delete group.'));
  271. }
  272. $groupModel->delete();
  273. $this->groupRegistry->remove($id);
  274. return true;
  275. }
  276. /**
  277. * Validate group values.
  278. *
  279. * @param \Magento\Customer\Api\Data\GroupInterface $group
  280. * @throws InputException
  281. * @return void
  282. *
  283. * @SuppressWarnings(PHPMD.NPathComplexity)
  284. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  285. */
  286. private function _validate($group)
  287. {
  288. $exception = new InputException();
  289. if (!\Zend_Validate::is($group->getCode(), 'NotEmpty')) {
  290. $exception->addError(__(InputException::REQUIRED_FIELD, ['fieldName' => 'code']));
  291. }
  292. if ($exception->wasErrorAdded()) {
  293. throw $exception;
  294. }
  295. }
  296. /**
  297. * Verifies that the tax class model exists and is a customer tax class type.
  298. *
  299. * @param int $taxClassId The id of the tax class model to check
  300. * @param \Magento\Customer\Api\Data\GroupInterface $group The original group parameters
  301. * @return void
  302. * @throws InputException Thrown if the tax class model is invalid
  303. */
  304. protected function _verifyTaxClassModel($taxClassId, $group)
  305. {
  306. try {
  307. /* @var TaxClassInterface $taxClassData */
  308. $taxClassData = $this->taxClassRepository->get($taxClassId);
  309. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  310. throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
  311. }
  312. if ($taxClassData->getClassType() !== TaxClassManagementInterface::TYPE_CUSTOMER) {
  313. throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
  314. }
  315. }
  316. }