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