Group.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. /**
  8. * Customer group model
  9. *
  10. * @api
  11. * @method string getCustomerGroupCode()
  12. * @method \Magento\Customer\Model\Group setCustomerGroupCode(string $value)
  13. * @method \Magento\Customer\Model\Group setTaxClassId(int $value)
  14. * @method Group setTaxClassName(string $value)
  15. * @since 100.0.2
  16. */
  17. class Group extends \Magento\Framework\Model\AbstractModel
  18. {
  19. const NOT_LOGGED_IN_ID = 0;
  20. const CUST_GROUP_ALL = 32000;
  21. const ENTITY = 'customer_group';
  22. const GROUP_CODE_MAX_LENGTH = 32;
  23. /**
  24. * Prefix of model events names
  25. *
  26. * @var string
  27. */
  28. protected $_eventPrefix = 'customer_group';
  29. /**
  30. * Parameter name in event
  31. *
  32. * In observe method you can use $observer->getEvent()->getObject() in this case
  33. *
  34. * @var string
  35. */
  36. protected $_eventObject = 'object';
  37. /**
  38. * @var \Magento\Store\Model\StoresConfig
  39. */
  40. protected $_storesConfig;
  41. /**
  42. * @var \Magento\Framework\Reflection\DataObjectProcessor
  43. */
  44. protected $dataObjectProcessor;
  45. /**
  46. * @var \Magento\Tax\Model\ClassModelFactory
  47. */
  48. protected $classModelFactory;
  49. /**
  50. * Constructor
  51. *
  52. * @param \Magento\Framework\Model\Context $context
  53. * @param \Magento\Framework\Registry $registry
  54. * @param \Magento\Store\Model\StoresConfig $storesConfig
  55. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  56. * @param \Magento\Tax\Model\ClassModelFactory $classModelFactory
  57. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  58. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  59. * @param array $data
  60. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  61. */
  62. public function __construct(
  63. \Magento\Framework\Model\Context $context,
  64. \Magento\Framework\Registry $registry,
  65. \Magento\Store\Model\StoresConfig $storesConfig,
  66. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  67. \Magento\Tax\Model\ClassModelFactory $classModelFactory,
  68. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  69. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  70. array $data = []
  71. ) {
  72. $this->_storesConfig = $storesConfig;
  73. $this->dataObjectProcessor = $dataObjectProcessor;
  74. $this->classModelFactory = $classModelFactory;
  75. parent::__construct(
  76. $context,
  77. $registry,
  78. $resource,
  79. $resourceCollection,
  80. $data
  81. );
  82. }
  83. /**
  84. * @return void
  85. */
  86. protected function _construct()
  87. {
  88. $this->_init(\Magento\Customer\Model\ResourceModel\Group::class);
  89. }
  90. /**
  91. * Alias for setCustomerGroupCode
  92. *
  93. * @param string $value
  94. * @return $this
  95. */
  96. public function setCode($value)
  97. {
  98. return $this->setCustomerGroupCode($value);
  99. }
  100. /**
  101. * Alias for getCustomerGroupCode
  102. *
  103. * @return string
  104. */
  105. public function getCode()
  106. {
  107. return $this->getCustomerGroupCode();
  108. }
  109. /**
  110. * Get tax class name
  111. *
  112. * @return string
  113. */
  114. public function getTaxClassName()
  115. {
  116. $taxClassName = $this->getData('tax_class_name');
  117. if ($taxClassName) {
  118. return $taxClassName;
  119. }
  120. $classModel = $this->classModelFactory->create();
  121. $classModel->load($this->getTaxClassId());
  122. $taxClassName = $classModel->getClassName();
  123. $this->setData('tax_class_name', $taxClassName);
  124. return $taxClassName;
  125. }
  126. /**
  127. * Determine if this group is used as the create account default group
  128. *
  129. * @return bool
  130. */
  131. public function usesAsDefault()
  132. {
  133. $data = $this->_storesConfig->getStoresConfigByPath(
  134. GroupManagement::XML_PATH_DEFAULT_ID
  135. );
  136. if (in_array($this->getId(), $data)) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Prepare data before save
  143. *
  144. * @return $this
  145. */
  146. public function beforeSave()
  147. {
  148. $this->_prepareData();
  149. return parent::beforeSave();
  150. }
  151. /**
  152. * Prepare customer group data
  153. *
  154. * @return $this
  155. */
  156. protected function _prepareData()
  157. {
  158. $this->setCode(substr($this->getCode(), 0, self::GROUP_CODE_MAX_LENGTH));
  159. return $this;
  160. }
  161. }