PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Eav/Model/Entity/Type.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 386 lines | 179 code | 39 blank | 168 comment | 15 complexity | 9e5c1fee351daf9cd8251a98c3fa8bbd MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Entity;
  7. /**
  8. * Entity type model
  9. *
  10. * @method \Magento\Eav\Model\ResourceModel\Entity\Type _getResource()
  11. * @method \Magento\Eav\Model\ResourceModel\Entity\Type getResource()
  12. * @method \Magento\Eav\Model\Entity\Type setEntityTypeCode(string $value)
  13. * @method string getEntityModel()
  14. * @method \Magento\Eav\Model\Entity\Type setEntityModel(string $value)
  15. * @method \Magento\Eav\Model\Entity\Type setAttributeModel(string $value)
  16. * @method \Magento\Eav\Model\Entity\Type setEntityTable(string $value)
  17. * @method \Magento\Eav\Model\Entity\Type setValueTablePrefix(string $value)
  18. * @method \Magento\Eav\Model\Entity\Type setEntityIdField(string $value)
  19. * @method int getIsDataSharing()
  20. * @method \Magento\Eav\Model\Entity\Type setIsDataSharing(int $value)
  21. * @method string getDataSharingKey()
  22. * @method \Magento\Eav\Model\Entity\Type setDataSharingKey(string $value)
  23. * @method \Magento\Eav\Model\Entity\Type setDefaultAttributeSetId(int $value)
  24. * @method string getIncrementModel()
  25. * @method \Magento\Eav\Model\Entity\Type setIncrementModel(string $value)
  26. * @method int getIncrementPerStore()
  27. * @method \Magento\Eav\Model\Entity\Type setIncrementPerStore(int $value)
  28. * @method int getIncrementPadLength()
  29. * @method \Magento\Eav\Model\Entity\Type setIncrementPadLength(int $value)
  30. * @method string getIncrementPadChar()
  31. * @method \Magento\Eav\Model\Entity\Type setIncrementPadChar(string $value)
  32. * @method string getAdditionalAttributeTable()
  33. * @method \Magento\Eav\Model\Entity\Type setAdditionalAttributeTable(string $value)
  34. * @method \Magento\Eav\Model\Entity\Type setEntityAttributeCollection(string $value)
  35. *
  36. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  37. */
  38. class Type extends \Magento\Framework\Model\AbstractModel
  39. {
  40. /**
  41. * Collection of attributes
  42. *
  43. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  44. */
  45. protected $_attributes;
  46. /**
  47. * Array of attributes
  48. *
  49. * @var array
  50. */
  51. protected $_attributesBySet = [];
  52. /**
  53. * Collection of sets
  54. *
  55. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection
  56. */
  57. protected $_sets;
  58. /**
  59. * @var \Magento\Eav\Model\Entity\AttributeFactory
  60. */
  61. protected $_attributeFactory;
  62. /**
  63. * @var \Magento\Eav\Model\Entity\Attribute\SetFactory
  64. */
  65. protected $_attSetFactory;
  66. /**
  67. * @var \Magento\Eav\Model\Entity\StoreFactory
  68. */
  69. protected $_storeFactory;
  70. /**
  71. * @var \Magento\Framework\Validator\UniversalFactory
  72. */
  73. protected $_universalFactory;
  74. /**
  75. * @param \Magento\Framework\Model\Context $context
  76. * @param \Magento\Framework\Registry $registry
  77. * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
  78. * @param \Magento\Eav\Model\Entity\Attribute\SetFactory $attSetFactory
  79. * @param \Magento\Eav\Model\Entity\StoreFactory $storeFactory
  80. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  81. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  82. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  83. * @param array $data
  84. * @codeCoverageIgnore
  85. */
  86. public function __construct(
  87. \Magento\Framework\Model\Context $context,
  88. \Magento\Framework\Registry $registry,
  89. \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
  90. \Magento\Eav\Model\Entity\Attribute\SetFactory $attSetFactory,
  91. \Magento\Eav\Model\Entity\StoreFactory $storeFactory,
  92. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  93. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  94. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  95. array $data = []
  96. ) {
  97. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  98. $this->_attributeFactory = $attributeFactory;
  99. $this->_attSetFactory = $attSetFactory;
  100. $this->_storeFactory = $storeFactory;
  101. $this->_universalFactory = $universalFactory;
  102. }
  103. /**
  104. * Resource initialization
  105. *
  106. * @return void
  107. * @codeCoverageIgnore
  108. */
  109. protected function _construct()
  110. {
  111. $this->_init(\Magento\Eav\Model\ResourceModel\Entity\Type::class);
  112. }
  113. /**
  114. * Load type by code
  115. *
  116. * @param string $code
  117. * @return $this
  118. */
  119. public function loadByCode($code)
  120. {
  121. $this->_getResource()->loadByCode($this, $code);
  122. $this->_afterLoad();
  123. return $this;
  124. }
  125. /**
  126. * Retrieve entity type attributes collection
  127. *
  128. * @param int $setId
  129. * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  130. */
  131. public function getAttributeCollection($setId = null)
  132. {
  133. if ($setId === null) {
  134. if ($this->_attributes === null) {
  135. $this->_attributes = $this->_getAttributeCollection()->setEntityTypeFilter($this);
  136. }
  137. $collection = $this->_attributes;
  138. } else {
  139. if (!isset($this->_attributesBySet[$setId])) {
  140. $this->_attributesBySet[$setId] = $this->_getAttributeCollection()->setEntityTypeFilter(
  141. $this
  142. )->setAttributeSetFilter(
  143. $setId
  144. );
  145. }
  146. $collection = $this->_attributesBySet[$setId];
  147. }
  148. return $collection;
  149. }
  150. /**
  151. * Init and retrieve attribute collection
  152. *
  153. * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
  154. */
  155. protected function _getAttributeCollection()
  156. {
  157. $collection = $this->_attributeFactory->create()->getCollection();
  158. $objectsModel = $this->getAttributeModel();
  159. if ($objectsModel) {
  160. $collection->setModel($objectsModel);
  161. }
  162. return $collection;
  163. }
  164. /**
  165. * Retrieve entity tpe sets collection
  166. *
  167. * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection
  168. */
  169. public function getAttributeSetCollection()
  170. {
  171. if (empty($this->_sets)) {
  172. $this->_sets = $this->_attSetFactory->create()->getResourceCollection()->setEntityTypeFilter(
  173. $this->getId()
  174. );
  175. }
  176. return $this->_sets;
  177. }
  178. /**
  179. * Retrieve new incrementId
  180. *
  181. * @param int $storeId
  182. * @return string
  183. * @throws \Exception
  184. */
  185. public function fetchNewIncrementId($storeId = null)
  186. {
  187. if (!$this->getIncrementModel()) {
  188. return false;
  189. }
  190. if (!$this->getIncrementPerStore() || $storeId === null) {
  191. /**
  192. * store_id null we can have for entity from removed store
  193. */
  194. $storeId = 0;
  195. }
  196. // Start transaction to run SELECT ... FOR UPDATE
  197. $this->_getResource()->beginTransaction();
  198. try {
  199. $entityStoreConfig = $this->_storeFactory->create()->loadByEntityStore($this->getId(), $storeId);
  200. if (!$entityStoreConfig->getId()) {
  201. $entityStoreConfig->setEntityTypeId(
  202. $this->getId()
  203. )->setStoreId(
  204. $storeId
  205. )->setIncrementPrefix(
  206. $storeId
  207. )->save();
  208. }
  209. $incrementInstance = $this->_universalFactory->create(
  210. $this->getIncrementModel()
  211. )->setPrefix(
  212. $entityStoreConfig->getIncrementPrefix()
  213. )->setPadLength(
  214. $this->getIncrementPadLength()
  215. )->setPadChar(
  216. $this->getIncrementPadChar()
  217. )->setLastId(
  218. $entityStoreConfig->getIncrementLastId()
  219. )->setEntityTypeId(
  220. $entityStoreConfig->getEntityTypeId()
  221. )->setStoreId(
  222. $entityStoreConfig->getStoreId()
  223. );
  224. /**
  225. * do read lock on eav/entity_store to solve potential timing issues
  226. * (most probably already done by beginTransaction of entity save)
  227. */
  228. $incrementId = $incrementInstance->getNextId();
  229. $entityStoreConfig->setIncrementLastId($incrementId);
  230. $entityStoreConfig->save();
  231. // Commit increment_last_id changes
  232. $this->_getResource()->commit();
  233. } catch (\Exception $exception) {
  234. $this->_getResource()->rollBack();
  235. throw $exception;
  236. }
  237. return $incrementId;
  238. }
  239. /**
  240. * Retrieve entity id field
  241. *
  242. * @return string|null
  243. */
  244. public function getEntityIdField()
  245. {
  246. return isset($this->_data['entity_id_field']) ? $this->_data['entity_id_field'] : null;
  247. }
  248. /**
  249. * Retrieve entity table name
  250. *
  251. * @return string|null
  252. */
  253. public function getEntityTable()
  254. {
  255. if (isset($this->_data['entity_table'])) {
  256. return $this->getResource()->getTable($this->_data['entity_table']);
  257. }
  258. return null;
  259. }
  260. /**
  261. * Retrieve entity table prefix name
  262. *
  263. * @return null|string
  264. */
  265. public function getValueTablePrefix()
  266. {
  267. $prefix = $this->getEntityTablePrefix();
  268. if ($prefix) {
  269. return $this->getResource()->getTable($prefix);
  270. }
  271. return null;
  272. }
  273. /**
  274. * Retrieve entity table prefix
  275. *
  276. * @return string
  277. */
  278. public function getEntityTablePrefix()
  279. {
  280. $tablePrefix = trim($this->_data['value_table_prefix']);
  281. if (empty($tablePrefix)) {
  282. $tablePrefix = $this->getEntityTable();
  283. }
  284. return $tablePrefix;
  285. }
  286. /**
  287. * Get default attribute set identifier for etity type
  288. *
  289. * @return string|null
  290. */
  291. public function getDefaultAttributeSetId()
  292. {
  293. return isset($this->_data['default_attribute_set_id']) ? $this->_data['default_attribute_set_id'] : null;
  294. }
  295. /**
  296. * Retrieve entity type id
  297. *
  298. * @return string|null
  299. */
  300. public function getEntityTypeId()
  301. {
  302. return isset($this->_data['entity_type_id']) ? $this->_data['entity_type_id'] : null;
  303. }
  304. /**
  305. * Retrieve entity type code
  306. *
  307. * @return string|null
  308. */
  309. public function getEntityTypeCode()
  310. {
  311. return isset($this->_data['entity_type_code']) ? $this->_data['entity_type_code'] : null;
  312. }
  313. /**
  314. * Get attribute model code for entity type
  315. *
  316. * @return string
  317. */
  318. public function getAttributeModel()
  319. {
  320. if (empty($this->_data['attribute_model'])) {
  321. return \Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL;
  322. }
  323. return $this->_data['attribute_model'];
  324. }
  325. /**
  326. * Retrieve resource entity object
  327. *
  328. * @return \Magento\Framework\Model\ResourceModel\AbstractResource
  329. */
  330. public function getEntity()
  331. {
  332. return $this->_universalFactory->create($this->_data['entity_model']);
  333. }
  334. /**
  335. * Return attribute collection. If not specify return default
  336. *
  337. * @return string
  338. */
  339. public function getEntityAttributeCollection()
  340. {
  341. $collection = $this->_getData('entity_attribute_collection');
  342. if ($collection) {
  343. return $collection;
  344. }
  345. return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class;
  346. }
  347. }