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