/** * Notify all observers with an event and the associate object/data that observers might find useful. * @param string $event The event we wish to signal. * @param mixed $object The object/data that we want to make avaliable to the observers. */ public function notify($event, $object) { $eventObject = new Event($event, $object); if (isset($this->_observers[$event]) && is_array($this->_observers[$event])) { foreach ($this->_observers[$event] as $observer) { if ($eventObject->propagationStopped()) { break; } $observer->onEvent($eventObject); } } }
/** * When this pod recieves an event, process it accordingly. * In this case, the pod simply calls the various functions in model before and after each event. * @param Event $eventObject The event object. */ public function onEvent(Event $eventObject) { if ($eventObject->getObject() === $this) { switch ($eventObject->getEvent()) { case 'after_dispense': $this->getModel()->afterDispense(); break; case 'after_open': $this->getModel()->afterOpen(); break; case 'before_store': $this->getModel()->beforeStore(); break; case 'after_store': $this->getModel()->afterStore(); break; case 'before_delete': $this->getModel()->beforeDelete(); break; case 'after_delete': $this->getModel()->afterDelete(); break; } } }
/** * @covers Paradox\Event::propagationStopped */ public function testPropagationStopped() { $this->assertFalse($this->event->propagationStopped(), "The propagation should not be stopped"); $this->event->stopPropagation(); $this->assertTrue($this->event->propagationStopped(), "The propagation should be stopped"); }