for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\EventProjector\EventHandlers;
use Exception;
use Spatie\EventProjector\Exceptions\InvalidEventHandler;
use Spatie\EventProjector\Models\StoredEvent;
use Illuminate\Support\Collection;
trait HandlesEvents
{
public function handles(): Collection
return $this->getEventHandlingMethods()->keys();
}
public function handle(StoredEvent $storedEvent)
$eventClass = $storedEvent->event_class;
$handlerMethod = $this->getEventHandlingMethods()->get($eventClass);
if (! method_exists($this, $handlerMethod)) {
throw InvalidEventHandler::eventHandlingMethodDoesNotExist($this, $storedEvent->event, $handlerMethod);
app()->call([$this, $handlerMethod], [
'event' => $storedEvent->event,
'storedEvent' => $storedEvent,
]);
public function handleException(Exception $exception)
report($exception);
protected function getEventHandlingMethods(): Collection
return collect($this->handlesEvents ?? [])
handlesEvents
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
->mapWithKeys(function (string $handlerMethod, $eventClass) {
if (is_numeric($eventClass)) {
return [$handlerMethod => 'on'.ucfirst(class_basename($handlerMethod))];
return [$eventClass => $handlerMethod];
});
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: