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