Page MenuHomeDevCentral
Paste P307

HandleMethodExampleForNotificationListener.php
ActivePublic

Authored by dereckson on May 7 2022, 17:22.
Tags
None
Referenced Files
F1836679: HandleMethodExampleForNotificationListener.php
May 7 2022, 17:22
Subscribers
None
<?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);
}