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