Transaction.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * DB transaction model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\DB;
  9. /**
  10. * @todo need collect connection by name
  11. */
  12. class Transaction
  13. {
  14. /**
  15. * Objects which will be involved to transaction
  16. *
  17. * @var array
  18. */
  19. protected $_objects = [];
  20. /**
  21. * Transaction objects array with alias key
  22. *
  23. * @var array
  24. */
  25. protected $_objectsByAlias = [];
  26. /**
  27. * Callbacks array.
  28. *
  29. * @var array
  30. */
  31. protected $_beforeCommitCallbacks = [];
  32. /**
  33. * Begin transaction for all involved object resources
  34. *
  35. * @return $this
  36. */
  37. protected function _startTransaction()
  38. {
  39. foreach ($this->_objects as $object) {
  40. $object->getResource()->beginTransaction();
  41. }
  42. return $this;
  43. }
  44. /**
  45. * Commit transaction for all resources
  46. *
  47. * @return $this
  48. */
  49. protected function _commitTransaction()
  50. {
  51. foreach ($this->_objects as $object) {
  52. $object->getResource()->commit();
  53. }
  54. return $this;
  55. }
  56. /**
  57. * Rollback transaction
  58. *
  59. * @return $this
  60. */
  61. protected function _rollbackTransaction()
  62. {
  63. foreach ($this->_objects as $object) {
  64. $object->getResource()->rollBack();
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Run all configured object callbacks
  70. *
  71. * @return $this
  72. */
  73. protected function _runCallbacks()
  74. {
  75. foreach ($this->_beforeCommitCallbacks as $callback) {
  76. call_user_func($callback);
  77. }
  78. return $this;
  79. }
  80. /**
  81. * Adding object for using in transaction
  82. *
  83. * @param \Magento\Framework\Model\AbstractModel $object
  84. * @param string $alias
  85. * @return $this
  86. */
  87. public function addObject(\Magento\Framework\Model\AbstractModel $object, $alias = '')
  88. {
  89. $this->_objects[] = $object;
  90. if (!empty($alias)) {
  91. $this->_objectsByAlias[$alias] = $object;
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Add callback function which will be called before commit transactions
  97. *
  98. * @param callback $callback
  99. * @return $this
  100. */
  101. public function addCommitCallback($callback)
  102. {
  103. $this->_beforeCommitCallbacks[] = $callback;
  104. return $this;
  105. }
  106. /**
  107. * Initialize objects save transaction
  108. *
  109. * @return $this
  110. * @throws \Exception
  111. */
  112. public function save()
  113. {
  114. $this->_startTransaction();
  115. $error = false;
  116. try {
  117. foreach ($this->_objects as $object) {
  118. $object->save();
  119. }
  120. } catch (\Exception $e) {
  121. $error = $e;
  122. }
  123. if ($error === false) {
  124. try {
  125. $this->_runCallbacks();
  126. } catch (\Exception $e) {
  127. $error = $e;
  128. }
  129. }
  130. if ($error) {
  131. $this->_rollbackTransaction();
  132. throw $error;
  133. } else {
  134. $this->_commitTransaction();
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Initialize objects delete transaction
  140. *
  141. * @return $this
  142. * @throws \Exception
  143. */
  144. public function delete()
  145. {
  146. $this->_startTransaction();
  147. $error = false;
  148. try {
  149. foreach ($this->_objects as $object) {
  150. $object->delete();
  151. }
  152. } catch (\Exception $e) {
  153. $error = $e;
  154. }
  155. if ($error === false) {
  156. try {
  157. $this->_runCallbacks();
  158. } catch (\Exception $e) {
  159. $error = $e;
  160. }
  161. }
  162. if ($error) {
  163. $this->_rollbackTransaction();
  164. throw $error;
  165. } else {
  166. $this->_commitTransaction();
  167. }
  168. return $this;
  169. }
  170. }