Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Trigger.php
Go to the documentation of this file.
1 <?php
7 
12 class Trigger
13 {
17  const TIME_BEFORE = 'BEFORE';
18 
19  const TIME_AFTER = 'AFTER';
20 
26  const EVENT_INSERT = 'INSERT';
27 
28  const EVENT_UPDATE = 'UPDATE';
29 
30  const EVENT_DELETE = 'DELETE';
31 
36 
43 
49  protected $name;
50 
56  protected $time;
57 
63  protected $event;
64 
70  protected $tableName;
71 
77  protected $statements = [];
78 
86  public function setName($name)
87  {
88  if (!is_string($name)) {
89  throw new \InvalidArgumentException(
90  (string)new \Magento\Framework\Phrase('Trigger name should be a string')
91  );
92  }
93 
94  $this->name = strtolower($name);
95  return $this;
96  }
97 
104  public function getName()
105  {
106  if (empty($this->name)) {
107  throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger name is not defined'));
108  }
109  return $this->name;
110  }
111 
119  public function setTime($time)
120  {
121  if (in_array($time, self::$listOfTimes)) {
122  $this->time = strtoupper($time);
123  } else {
124  throw new \InvalidArgumentException((string)new \Magento\Framework\Phrase('Trigger unsupported time type'));
125  }
126  return $this;
127  }
128 
135  public function getTime()
136  {
137  if ($this->time === null) {
138  throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger time is not defined'));
139  }
140  return $this->time;
141  }
142 
150  public function setEvent($event)
151  {
152  if (in_array($event, self::$listOfEvents)) {
153  $this->event = strtoupper($event);
154  } else {
155  throw new \InvalidArgumentException(
156  (string)new \Magento\Framework\Phrase('Trigger unsupported event type')
157  );
158  }
159  return $this;
160  }
161 
168  public function getEvent()
169  {
170  if ($this->event === null) {
171  throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger event is not defined'));
172  }
173  return $this->event;
174  }
175 
183  public function setTable($name)
184  {
185  if (!is_string($name)) {
186  throw new \InvalidArgumentException(
187  (string)new \Magento\Framework\Phrase('Trigger table name should be a string')
188  );
189  }
190  $this->tableName = $name;
191  return $this;
192  }
193 
200  public function getTable()
201  {
202  if (empty($this->tableName)) {
203  throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger table name is not defined'));
204  }
205  return $this->tableName;
206  }
207 
215  public function addStatement($statement)
216  {
217  if (!is_string($statement)) {
218  throw new \InvalidArgumentException(
219  (string)new \Magento\Framework\Phrase('Trigger statement should be a string')
220  );
221  }
222 
223  $statement = trim($statement);
224  $statement = rtrim($statement, ';') . ';';
225 
226  $this->statements[] = $statement;
227 
228  return $this;
229  }
230 
236  public function getStatements()
237  {
238  return $this->statements;
239  }
240 
246  public static function getListOfTimes()
247  {
248  return self::$listOfTimes;
249  }
250 
256  public static function getListOfEvents()
257  {
258  return self::$listOfEvents;
259  }
260 }