Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Observer.php
Go to the documentation of this file.
1 <?php
7 
9 
15 {
22  public function isValidFor(Event $event)
23  {
24  return $this->getEventName() === $event->getName();
25  }
26 
33  public function dispatch(Event $event)
34  {
35  if (!$this->isValidFor($event)) {
36  return $this;
37  }
38 
39  $callback = $this->getCallback();
40  $this->setEvent($event);
41 
42  $_profilerKey = 'OBSERVER: ';
43  if (is_object($callback[0])) {
44  $_profilerKey .= get_class($callback[0]);
45  } else {
46  $_profilerKey .= (string)$callback[0];
47  }
48  $_profilerKey .= ' -> ' . $callback[1];
49 
50  \Magento\Framework\Profiler::start($_profilerKey);
51  call_user_func($callback, $this);
52  \Magento\Framework\Profiler::stop($_profilerKey);
53 
54  return $this;
55  }
56 
60  public function getName()
61  {
62  return $this->getData('name');
63  }
64 
69  public function setName($data)
70  {
71  return $this->setData('name', $data);
72  }
73 
77  public function getEventName()
78  {
79  return $this->getData('event_name');
80  }
81 
86  public function setEventName($data)
87  {
88  return $this->setData('event_name', $data);
89  }
90 
94  public function getCallback()
95  {
96  return $this->getData('callback');
97  }
98 
103  public function setCallback($data)
104  {
105  return $this->setData('callback', $data);
106  }
107 
113  public function getEvent()
114  {
115  return $this->getData('event');
116  }
117 
122  public function setEvent($data)
123  {
124  return $this->setData('event', $data);
125  }
126 }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72