mercredi 15 août 2018

Laravel: safe to use event listener manually?

I have an event listener that doesn't implement ShouldQueue, but I'm planning on making it queueable later on. There is one event, onSignUp that has some code that I'd like to re-use synchronously in addition to the event-based paradigm now and also when ShouldQueue is implemented.

class TransactionEventListener implements ShouldQueue
{
    public function onSignUp(Signup $event)
    {
        // Code I'd like to re-use
    }

    public function subscribe($events)
    {
        $events->listen(Signup::class, self::class . '@onSignUp');
    }
}

To re-use onSignUp, I'm doing the following:

$listener = new TransactionEventListener();
$event = new Signup($data);
$listener->onSignUp($event);

Is it safe to use onSignUp like this when ShouldQueue is implemented, too? It's just plain PHP code, so I would think it should be fine later on, but I don't know what kind of magic Laravel does with ShouldQueue, and I'd like the transition when it happens to not break existing code.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire