Trigger.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Ddl;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Trigger
  12. {
  13. /**#@+
  14. * Trigger times
  15. */
  16. const TIME_BEFORE = 'BEFORE';
  17. const TIME_AFTER = 'AFTER';
  18. /**#@-*/
  19. /**#@+
  20. * Trigger events
  21. */
  22. const EVENT_INSERT = 'INSERT';
  23. const EVENT_UPDATE = 'UPDATE';
  24. const EVENT_DELETE = 'DELETE';
  25. /**#@-*/
  26. /**#@-*/
  27. protected static $listOfTimes = [self::TIME_BEFORE, self::TIME_AFTER];
  28. /**
  29. * List of events available for trigger
  30. *
  31. * @var array
  32. */
  33. protected static $listOfEvents = [self::EVENT_INSERT, self::EVENT_UPDATE, self::EVENT_DELETE];
  34. /**
  35. * Name of trigger
  36. *
  37. * @var string
  38. */
  39. protected $name;
  40. /**
  41. * Time of trigger
  42. *
  43. * @var string
  44. */
  45. protected $time;
  46. /**
  47. * Time of trigger
  48. *
  49. * @var string
  50. */
  51. protected $event;
  52. /**
  53. * Table name
  54. *
  55. * @var string
  56. */
  57. protected $tableName;
  58. /**
  59. * List of statements for trigger body
  60. *
  61. * @var array
  62. */
  63. protected $statements = [];
  64. /**
  65. * Set trigger name
  66. *
  67. * @param string $name
  68. * @throws \InvalidArgumentException
  69. * @return \Magento\Framework\DB\Ddl\Trigger
  70. */
  71. public function setName($name)
  72. {
  73. if (!is_string($name)) {
  74. throw new \InvalidArgumentException(
  75. (string)new \Magento\Framework\Phrase('Trigger name should be a string')
  76. );
  77. }
  78. $this->name = strtolower($name);
  79. return $this;
  80. }
  81. /**
  82. * Retrieve name of trigger
  83. *
  84. * @throws \Zend_Db_Exception
  85. * @return string
  86. */
  87. public function getName()
  88. {
  89. if (empty($this->name)) {
  90. throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger name is not defined'));
  91. }
  92. return $this->name;
  93. }
  94. /**
  95. * Set trigger time
  96. *
  97. * @param string $time
  98. * @throws \InvalidArgumentException
  99. * @return \Magento\Framework\DB\Ddl\Trigger
  100. */
  101. public function setTime($time)
  102. {
  103. if (in_array($time, self::$listOfTimes)) {
  104. $this->time = strtoupper($time);
  105. } else {
  106. throw new \InvalidArgumentException((string)new \Magento\Framework\Phrase('Trigger unsupported time type'));
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Retrieve time of trigger
  112. *
  113. * @throws \Zend_Db_Exception
  114. * @return string
  115. */
  116. public function getTime()
  117. {
  118. if ($this->time === null) {
  119. throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger time is not defined'));
  120. }
  121. return $this->time;
  122. }
  123. /**
  124. * Set trigger event
  125. *
  126. * @param string $event
  127. * @throws \InvalidArgumentException
  128. * @return \Magento\Framework\DB\Ddl\Trigger
  129. */
  130. public function setEvent($event)
  131. {
  132. if (in_array($event, self::$listOfEvents)) {
  133. $this->event = strtoupper($event);
  134. } else {
  135. throw new \InvalidArgumentException(
  136. (string)new \Magento\Framework\Phrase('Trigger unsupported event type')
  137. );
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Retrieve event of trigger
  143. *
  144. * @throws \Zend_Db_Exception
  145. * @return string
  146. */
  147. public function getEvent()
  148. {
  149. if ($this->event === null) {
  150. throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger event is not defined'));
  151. }
  152. return $this->event;
  153. }
  154. /**
  155. * Set table name
  156. *
  157. * @param string $name
  158. * @throws \InvalidArgumentException
  159. * @return \Magento\Framework\DB\Ddl\Trigger
  160. */
  161. public function setTable($name)
  162. {
  163. if (!is_string($name)) {
  164. throw new \InvalidArgumentException(
  165. (string)new \Magento\Framework\Phrase('Trigger table name should be a string')
  166. );
  167. }
  168. $this->tableName = $name;
  169. return $this;
  170. }
  171. /**
  172. * Retrieve table name
  173. *
  174. * @throws \Zend_Db_Exception
  175. * @return string
  176. */
  177. public function getTable()
  178. {
  179. if (empty($this->tableName)) {
  180. throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger table name is not defined'));
  181. }
  182. return $this->tableName;
  183. }
  184. /**
  185. * Add statement to trigger
  186. *
  187. * @param string $statement
  188. * @throws \InvalidArgumentException
  189. * @return \Magento\Framework\DB\Ddl\Trigger
  190. */
  191. public function addStatement($statement)
  192. {
  193. if (!is_string($statement)) {
  194. throw new \InvalidArgumentException(
  195. (string)new \Magento\Framework\Phrase('Trigger statement should be a string')
  196. );
  197. }
  198. $statement = trim($statement);
  199. $statement = rtrim($statement, ';') . ';';
  200. $this->statements[] = $statement;
  201. return $this;
  202. }
  203. /**
  204. * Retrieve list of statements of trigger
  205. *
  206. * @return array
  207. */
  208. public function getStatements()
  209. {
  210. return $this->statements;
  211. }
  212. /**
  213. * Retrieve list of times available for trigger
  214. *
  215. * @return array
  216. */
  217. public static function getListOfTimes()
  218. {
  219. return self::$listOfTimes;
  220. }
  221. /**
  222. * Retrieve list of events available for trigger
  223. *
  224. * @return array
  225. */
  226. public static function getListOfEvents()
  227. {
  228. return self::$listOfEvents;
  229. }
  230. }