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