Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Paste
P307
HandleMethodExampleForNotificationListener.php
Active
Public
Actions
Authored by
dereckson
on May 7 2022, 17:22.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
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
);
}
Event Timeline
dereckson
created this paste.
May 7 2022, 17:22
2022-05-07 17:22:04 (UTC+0)
Log In to Comment