<?php namespace Bixo\Group\Listeners; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Str; use Bixo\Group\Events\GroupWorkflow as GroupWorkflowEvent; use Bixo\Group\Notifications\GroupWorkflow as GroupWorkflowNotification; use Litepie\Actions\Concerns\AsAction; class GroupWorkflow { use AsAction; private $allowedTransitions = [ 'before' => [], 'after' => ['publish', 'submit', 'approve'], ]; public function handle(GroupWorkflowEvent $event) { $function = Str::camel($event->transition); return $this->$function($event); } /** * Sends a notification to the client when the $group is submitted. * * @param GroupWorkflowEvent $event The event object. * @return void */ public function submit(GroupWorkflowEvent $event) { $client = $event->group->client; Notification::send($client, new GroupWorkflowNotification($event)); } /** * Sends a notification to the client when the $group is published. * * @param GroupWorkflowEvent $event The event object. * @return void */ public function publish(GroupWorkflowEvent $event) { $client = $event->group->client; Notification::send($client, new GroupWorkflowNotification($event)); } /** * Sends a notification to the client when the $group is approved. * * @param GroupWorkflowEvent $event The event object. * @return void */ public function approve(GroupWorkflowEvent $event) { $client = $event->group->client; Notification::send($client, new GroupWorkflowNotification($event)); } /** * Handles the $group workflow event as a listener. * * @param GroupWorkflowEvent $event The event object. * @return void */ public function asListener(GroupWorkflowEvent $event) { if (($event->when == 'before' && in_array($event->transition, $this->allowedTransitions['before']) || $event->when == 'after' && in_array($event->transition, $this->allowedTransitions['after'])) ) { return $this->handle($event); } } }