Meta.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesSequence\Model\ResourceModel;
  7. use Magento\Framework\Exception\LocalizedException as Exception;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Model\ResourceModel\Db\Context as DatabaseContext;
  10. use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile;
  11. use Magento\SalesSequence\Model\MetaFactory;
  12. use Magento\SalesSequence\Model\Profile as ModelProfile;
  13. /**
  14. * Class Meta represents metadata for sequence as sequence table and store id
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Meta extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  20. {
  21. /**
  22. * Event prefix
  23. *
  24. * @var string
  25. */
  26. protected $_eventPrefix = 'sales_sequence_meta';
  27. /**
  28. * @var ResourceProfile
  29. */
  30. protected $resourceProfile;
  31. /**
  32. * @var MetaFactory
  33. */
  34. protected $metaFactory;
  35. /**
  36. * @param DatabaseContext $context
  37. * @param MetaFactory $metaFactory
  38. * @param ResourceProfile $resourceProfile
  39. * @param string $connectionName
  40. */
  41. public function __construct(
  42. DatabaseContext $context,
  43. MetaFactory $metaFactory,
  44. ResourceProfile $resourceProfile,
  45. $connectionName = null
  46. ) {
  47. $this->metaFactory = $metaFactory;
  48. $this->resourceProfile = $resourceProfile;
  49. parent::__construct($context, $connectionName);
  50. }
  51. /**
  52. * Model initialization
  53. *
  54. * @return void
  55. */
  56. protected function _construct()
  57. {
  58. $this->_init('sales_sequence_meta', 'meta_id');
  59. }
  60. /**
  61. * Retrieves Metadata for entity by entity type and store id
  62. *
  63. * @param string $entityType
  64. * @param int $storeId
  65. * @return \Magento\SalesSequence\Model\Meta
  66. * @throws \Magento\Framework\Exception\LocalizedException
  67. */
  68. public function loadByEntityTypeAndStore($entityType, $storeId)
  69. {
  70. $meta = $this->metaFactory->create();
  71. $connection = $this->getConnection();
  72. $bind = ['entity_type' => $entityType, 'store_id' => $storeId];
  73. $select = $connection->select()->from(
  74. $this->getMainTable(),
  75. [$this->getIdFieldName()]
  76. )->where(
  77. 'entity_type = :entity_type AND store_id = :store_id'
  78. );
  79. $metaId = $connection->fetchOne($select, $bind);
  80. if ($metaId) {
  81. $this->load($meta, $metaId);
  82. }
  83. return $meta;
  84. }
  85. /**
  86. * Using for load sequence profile and setting it into metadata
  87. *
  88. * @param \Magento\Framework\Model\AbstractModel $object
  89. *
  90. * @return $this|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
  91. * @throws \Magento\Framework\Exception\LocalizedException
  92. */
  93. protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
  94. {
  95. $object->setData(
  96. 'active_profile',
  97. $this->resourceProfile->loadActiveProfile($object->getId())
  98. );
  99. return $this;
  100. }
  101. /**
  102. * Validate metadata and sequence before save
  103. *
  104. * @param \Magento\Framework\Model\AbstractModel $object
  105. * @return $this
  106. * @throws Exception
  107. * @throws NoSuchEntityException
  108. */
  109. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  110. {
  111. if (!$object->getData('active_profile') instanceof ModelProfile) {
  112. throw new NoSuchEntityException(
  113. __(
  114. "The entity sequence profile wasn't added to the meta active profile. "
  115. . "Verify the profile and try again."
  116. )
  117. );
  118. }
  119. if (!$object->getData('entity_type')
  120. || $object->getData('store_id') === null
  121. || !$object->getData('sequence_table')
  122. ) {
  123. throw new Exception(__('Not enough arguments'));
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Perform actions after object save
  129. *
  130. * @param \Magento\Framework\Model\AbstractModel $object
  131. *
  132. * @return $this|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
  133. * @throws \Magento\Framework\Exception\AlreadyExistsException
  134. */
  135. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  136. {
  137. $profile = $object->getData('active_profile')
  138. ->setMetaId($object->getId());
  139. $this->resourceProfile->save($profile);
  140. return $this;
  141. }
  142. }