mardi 30 octobre 2018

Laravel WorkflowSubscriber class that conditionally calls guard methods based on the current transition calling incorrect methods

I have an unnecessarily perplexing situation here. I'm simply trying to create a method that checks an events transition, creates a $guardMethod variable that will look something like guardToSomePlace and then if that method exists, call it. Here is the code for that concept:

<?php

namespace App\Listeners;

use App\Models\BugTypes\Bug;
use Symfony\Component\Workflow\Event\GuardEvent;

class BugWorkflowSubscriber
{
    /**
     * Handle workflow guard events.
     *
     * @param \Symfony\Component\Workflow\Event\GuardEvent $event
     */
    public function onGuard(GuardEvent $event)
    {
        $transition = $event->getTransition();
        $guardMethod = 'guard' . studly_case($transition->getName());

        if (method_exists($this, $guardMethod)) {
            $this->$guardMethod($event);
        }
    }

    /**
     * Guards the to_on_hold transition
     *
     * @param \Symfony\Workflow\Event\GuardEvent $event
     *
     * @return void
     */
    private function guardToOnHold(GuardEvent $event)
    {
        dd('why is it getting here for other transitions?');
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'workflow.bug.guard',
            'App\Listeners\BugWorkflowSubscriber@onGuard'
        );

        $events->listen(
            'workflow.bug.entered.*',
            'App\Listeners\Workflow\Bug\OnEntered'
        );
    }
}

It is calling the guardToOnHold method for multiple transitions, despite the fact that I can dd $event->getTransition() and it is the correct transition and if I dd method_exists($this, $guardMethod) it is false for any/all methods besides guardToOnHold, as that's the only one that would exist after I create the $guardMethod variable. If I dd method_exists($this, $guardMethod) before the if statement it is false, but dd-ing it inside the if statement with the same event/transition gets into the if statement and dd's true. This is Laravel 5.7 using the Brexis/Laravel Symfony Workflow package. The transition on the event is always what I expect it to be. $event->getTransition() always returns one Transition Object with the correct name, froms and tos.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2P35GMh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire