Builder.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesSequence\Model;
  7. use Magento\Framework\App\ResourceConnection as AppResource;
  8. use Magento\Framework\DB\Ddl\Sequence as DdlSequence;
  9. use Magento\Framework\Webapi\Exception;
  10. use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata;
  11. use Psr\Log\LoggerInterface as Logger;
  12. /**
  13. * Class Builder
  14. *
  15. * @api
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @since 100.0.2
  18. */
  19. class Builder
  20. {
  21. /**
  22. * @var resourceMetadata
  23. */
  24. protected $resourceMetadata;
  25. /**
  26. * @var ProfileFactory
  27. */
  28. protected $profileFactory;
  29. /**
  30. * @var MetaFactory
  31. */
  32. protected $metaFactory;
  33. /**
  34. * @var AppResource
  35. */
  36. protected $appResource;
  37. /**
  38. * @var DdlSequence
  39. */
  40. protected $ddlSequence;
  41. /**
  42. * List of required sequence attribute
  43. *
  44. * @var array
  45. */
  46. protected $required = [
  47. 'entityType',
  48. 'storeId'
  49. ];
  50. /**
  51. * Default pattern for sequence creation, full list of attributes that can be defined by customer
  52. *
  53. * @var array
  54. */
  55. protected $pattern = [
  56. 'entity_type',
  57. 'store_id',
  58. 'prefix',
  59. 'suffix',
  60. 'start_value',
  61. 'step',
  62. 'max_value',
  63. 'warning_value',
  64. ];
  65. /**
  66. * Concrete data of sequence
  67. *
  68. * @var array
  69. */
  70. protected $data = [];
  71. /**
  72. * @var Logger
  73. */
  74. protected $logger;
  75. /**
  76. * @param ResourceMetadata $resourceMetadata
  77. * @param MetaFactory $metaFactory
  78. * @param ProfileFactory $profileFactory
  79. * @param AppResource $appResource
  80. * @param DdlSequence $ddlSequence
  81. * @param Logger $logger
  82. */
  83. public function __construct(
  84. ResourceMetadata $resourceMetadata,
  85. MetaFactory $metaFactory,
  86. ProfileFactory $profileFactory,
  87. AppResource $appResource,
  88. DdlSequence $ddlSequence,
  89. Logger $logger
  90. ) {
  91. $this->resourceMetadata = $resourceMetadata;
  92. $this->metaFactory = $metaFactory;
  93. $this->profileFactory = $profileFactory;
  94. $this->appResource = $appResource;
  95. $this->ddlSequence = $ddlSequence;
  96. $this->logger = $logger;
  97. $this->data = array_flip($this->pattern);
  98. }
  99. /**
  100. * @param string $entityType
  101. * @return $this
  102. */
  103. public function setEntityType($entityType)
  104. {
  105. $this->data['entity_type'] = $entityType;
  106. return $this;
  107. }
  108. /**
  109. * @param int $storeId
  110. * @return $this
  111. */
  112. public function setStoreId($storeId)
  113. {
  114. $this->data['store_id'] = $storeId;
  115. return $this;
  116. }
  117. /**
  118. * @param string $prefix
  119. * @return $this
  120. */
  121. public function setPrefix($prefix)
  122. {
  123. $this->data['prefix'] = $prefix;
  124. return $this;
  125. }
  126. /**
  127. * @param string $suffix
  128. * @return $this
  129. */
  130. public function setSuffix($suffix)
  131. {
  132. $this->data['suffix'] = $suffix;
  133. return $this;
  134. }
  135. /**
  136. * @param int $startValue
  137. * @return $this
  138. */
  139. public function setStartValue($startValue)
  140. {
  141. $this->data['start_value'] = $startValue;
  142. return $this;
  143. }
  144. /**
  145. * @param int $step
  146. * @return $this
  147. */
  148. public function setStep($step)
  149. {
  150. $this->data['step'] = $step;
  151. return $this;
  152. }
  153. /**
  154. * @param int $maxValue
  155. * @return $this
  156. */
  157. public function setMaxValue($maxValue)
  158. {
  159. $this->data['max_value'] = $maxValue;
  160. return $this;
  161. }
  162. /**
  163. * @param int $warningValue
  164. * @return $this
  165. */
  166. public function setWarningValue($warningValue)
  167. {
  168. $this->data['warning_value'] = $warningValue;
  169. return $this;
  170. }
  171. /**
  172. * Returns sequence table name
  173. *
  174. * @return string
  175. */
  176. protected function getSequenceName()
  177. {
  178. return $this->appResource->getTableName(
  179. sprintf(
  180. 'sequence_%s_%s',
  181. $this->data['entity_type'],
  182. $this->data['store_id']
  183. )
  184. );
  185. }
  186. /**
  187. * Create sequence with metadata and profile
  188. *
  189. * @throws \Exception
  190. * @throws \Magento\Framework\Exception\AlreadyExistsException
  191. * @return void
  192. */
  193. public function create()
  194. {
  195. $metadata = $this->resourceMetadata->loadByEntityTypeAndStore(
  196. $this->data['entity_type'],
  197. $this->data['store_id']
  198. );
  199. if ($metadata->getSequenceTable() == $this->getSequenceName()) {
  200. return;
  201. }
  202. $this->data['sequence_table'] = $this->getSequenceName();
  203. $this->data['is_active'] = 1;
  204. $profile = $this->profileFactory->create(
  205. [
  206. 'data' => array_intersect_key(
  207. $this->data,
  208. array_flip(
  209. [
  210. 'prefix', 'suffix', 'start_value', 'step', 'max_value', 'warning_value',
  211. 'is_active', 'active_profile'
  212. ]
  213. )
  214. )
  215. ]
  216. );
  217. $profile->setHasDataChanges(true);
  218. $this->data['active_profile'] = $profile;
  219. $metadata = $this->metaFactory->create(
  220. [
  221. 'data' => array_intersect_key(
  222. $this->data,
  223. array_flip(['entity_type', 'store_id', 'sequence_table', 'active_profile'])
  224. )
  225. ]
  226. );
  227. $metadata->setHasDataChanges(true);
  228. try {
  229. $this->resourceMetadata->save($metadata);
  230. $connection = $this->appResource->getConnection('sales');
  231. if (!$connection->isTableExists($this->data['sequence_table'])) {
  232. $connection->query(
  233. $this->ddlSequence->getCreateSequenceDdl(
  234. $this->data['sequence_table'],
  235. $this->data['start_value']
  236. )
  237. );
  238. }
  239. } catch (Exception $e) {
  240. $this->resourceMetadata->delete($metadata);
  241. $this->logger->critical($e);
  242. throw $e;
  243. }
  244. $this->data = array_flip($this->pattern);
  245. }
  246. }