Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to listen transition guard event, and How guard is working here? #63

Open
pavan-cs opened this issue Sep 17, 2019 · 1 comment
Open

Comments

@pavan-cs
Copy link

i follow the read me instruction, #53
still not able to connect how will it listen the events.
here is my listner,

<?php 
namespace App\Listeners;

use Brexis\LaravelWorkflow\Events\GuardEvent;

class ApprovalWorkflowListeners
{
    /**
     * Handle workflow guard events.
     */
    public function onGuard(GuardEvent $event) {
        /** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
        /** @var App\BlogPost $post */
        $post = $originalEvent->getSubject();
        $title = $post->title;

        if (empty($title)) {
            // Posts with no title should not be allowed
            $originalEvent->setBlocked(true);
        }
    }

    /**
     * Handle workflow leave event.
     */
    public function onLeave($event) {
    	/** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow transition event.
     */
    public function onTransition($event) {
    	/** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow enter event.
     */
    public function onEnter($event) {
    	/** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Handle workflow entered event.
     */
    public function onEntered($event) {
    	/** Symfony\Component\Workflow\Event\GuardEvent */
        $originalEvent = $event->getOriginalEvent();
dd($originalEvent);
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
    	dd($events);
        $events->listen(
            'Brexis\LaravelWorkflow\Events\GuardEvent',
            'App\Listeners\ApprovalWorkflowListeners@onGuard'
        );

        $events->listen(
            'Brexis\LaravelWorkflow\Events\LeaveEvent',
            'App\Listeners\ApprovalWorkflowListeners@onLeave'
        );

        $events->listen(
            'Brexis\LaravelWorkflow\Events\TransitionEvent',
            'App\Listeners\ApprovalWorkflowListeners@onTransition'
        );

        $events->listen(
            'Brexis\LaravelWorkflow\Events\EnterEvent',
            'App\Listeners\ApprovalWorkflowListeners@onEnter'
        );

        $events->listen(
            'Brexis\LaravelWorkflow\Events\EnteredEvent',
            'App\Listeners\ApprovalWorkflowListeners@onEntered'
        );
    }

};

here my workflow config

<?php

return [
    'approval_workflow'   => [
        'type'          => 'workflow', //'state_machine',
        'marking_store' => [
            'type' => 'single_state', // 'multiple_state',
            'arguments' => ['status'],
        ],
        'supports'      => ['App\Plan'],
        'places'        => ['new', 'submitted', 'in_review_work', 'pending_for_review_l1', 'approved_l1', 'review_comments', 'rejected', 'pending_for_review_l2', 'approved_l2'],
        'transitions'   => [
            'submit' => [
                // ''
                'from' => 'new',
                'to'   => 'submitted',
            ],

            'review-pending-l1' =>[
                // 'guard'=> todo
                'from' => 'submitted',
                'to'   => 'pending_for_review_l1',
            ],
            
            'approve-l1' => [
                // 'guard'=> todo
                'from' => ['pending_for_review_l1'],
                'to'   => 'approved_l1',
            ],

            'review-pending-l2' => [
                'from' => ['approved_l1'],
                'to'   => 'pending_for_review_l2',
            ],

            'approve-l2' => [
                'from' => ['pending_for_review_l2'],
                'to'   => 'approved_l2',
            ],

            'review-comment' => [
                // 'guard'=>
                'from' => ['pending_for_review_l1','pending_for_review_l2'],
                'to'   => 'review_comments',
            ],

            'review-work' => [
                'from' => ['submitted','pending_for_review_l1','pending_for_review_l2','review_comments','rejected'],
                'to' => 'in_review_work',
            ],

            'reject' => [
                'from' => ['submitted','pending_for_review_l1','pending_for_review_l2'],
                'to' => 'rejected',
            ]
        ],
    ]
];
  

does event trigger automatically ?
In ApprovalWorkflowListeners onGuard should be called, i could validate and block the txn but no luck.

or we have to define in config like (symphony)
'review-pending-l1' =>[ 'guard'=> "has_role('customer')", 'from' => 'submitted', 'to' => 'pending_for_review_l1', ],
second, how can i implement guard in my workflow based on user role.
ex. if user has customer role he will see only submit only, and if user has managed role he will see approve, review, reject etc

@fabien44300
Copy link

Hello,

In App\Providers\EventServiceProvider file, you have to add your listener class into $subscribe :
protected $subscribe = [
'App\Listeners\ApprovalWorkflowListeners',
];

Fabien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants