I've got a fairly massive system running a lot of one action objects to keep my controllers cleaner. There's some logic in my action classes that could be refactored into events.
Question is, how much load would events cause if I use them all over the show, example below, I'm using RabbitMQ as a queue service, so events will already be queued.
Ignore naming of stuff, I just wrote the code without running etc.
<?php
namespace App\Actions;
abstract class ActionBase
{
protected function getEventName(): string
{
return snake_case(class_basename($this));
}
protected function firePreEvent(... $data): void
{
event("actions.{$this->getEventName()}.pre", $data);
}
public function firePostEvent(): void
{
event("actions.{$this->getEventName()}.post", $data);
}
public function fireErrorEvent(): void
{
event("actions.{$this->getEventName()}.error", $data);
}
}
final class CreateUser extends ActionBase
{
public function execute(array $data)
{
$this->firePreEvent($data);
// doing something
if($error == true)
{
$this->fireErrorEvent($data);
return false;
}
$processed_data = ['foo' => 'bar'];
$this->firePostEvent($processed_data);
}
}
Event Service Provider
protected $listen = [
'actions.*.pre' => [
'App\Listeners\Auditing\PreActionAudit',
],
'actions.*.post' => [
'App\Listeners\Auditing\PostActionAudit',
],
'actions.*.error' => [
'App\Listeners\Auditing\ErrorActionAudit',
],
'actions.create_user.post' => [
'App\Listeners\Notifications\EmailUser'
],
'actions.create_user.error' => [
'App\Listeners\Notifications\EmailOnFailure',
'App\Notifications\SlackNotice',
'App\Listeners\LogIt',
],
]