/** * return a read-only list of event handlers for a given event from * a given event stream * * @param EventStream $stream * the stream to extract from * @param string $eventName * the event to get the handler list for * @return array<callable> * the list of handlers for the event * can be an empty list */ public static function from(EventStream $stream, $eventName) { // defensive programming RequireEventName::check($eventName); if (!isset($stream->{$eventName})) { return []; } return $stream->{$eventName}; }
/** * We want $eventHandler to be called whenever an event of type $eventName * is dispatch to the event stream $stream * * @param EventStream $stream * the stream we want to register the event handler on * @param string $eventName * the event we want to handle * @param callable $eventHandler * the handler we want called * @return void */ public static function on(EventStream $stream, $eventName, $eventHandler) { RequireEventName::check($eventName); RequireCallable::check($eventHandler, E4xx_UnsupportedType::class); if (!isset($stream->{$eventName})) { $stream->{$eventName} = []; } $stream->{$eventName}[] = $eventHandler; }
/** * @covers ::check * @dataProvider provideEverythingElse * @expectedException GanbaroDigital\EventStream\Exceptions\E4xx_NotAnEvent */ public function testThrowsExceptionForEverythingElse($eventName) { // ---------------------------------------------------------------- // setup your test // ---------------------------------------------------------------- // perform the change RequireEventName::check($eventName); // ---------------------------------------------------------------- // test the results // // if we get here with no exception, the test has failed }