Page MenuHomeDevCentral
Paste P307

HandleMethodExampleForNotificationListener.php
ActivePublic

Authored by dereckson on Sat, May 7, 17:22.
<?php
// NotificationListener: how to call the right on...... method?
use Nasqueron\Notifications\Events\Event;
private static function getEventHandlerMethod (string $eventClasss) : string {
$parts = explode('\\', $class);
$className = end($parts);
if (!str_ends_with($className, "Event")) {
throw new InvalidArgumentException("Events classes must be ended by 'Event'");
}
return "on" . substr($className, 0, 16);
}
public function handle (Event $event) : void {
$callable = [$this, self::getEventHandlerMethod($event::class)]
$callable($event);
}