<?php namespace Eform\Instruction\Listeners; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Str; use Eform\Instruction\Events\InstructionCategoryAction as InstructionCategoryActionEvent; use Eform\Instruction\Notifications\InstructionCategoryAction as InstructionCategoryActionNotification; use Litepie\Actions\Concerns\AsAction; class InstructionCategoryAction { use AsAction; private $allowedActions = [ 'before' => [], 'after' => ['create'], ]; /** * Handle the InstructionCategoryActionEvent. * * @param InstructionCategoryActionEvent $event * @return mixed */ public function handle(InstructionCategoryActionEvent $event) { $function = Str::camel($event->action); return $this->$function($event); } /** * Create a new $instruction_category. * * @param InstructionCategoryActionEvent $event * @return void */ public function create(InstructionCategoryActionEvent $event) { $client = $event->instruction_category->client; Notification::send($client, new InstructionCategoryActionNotification($event)); } /** * Handle the InstructionCategoryActionEvent as a listener. * * @param InstructionCategoryActionEvent $event * @return mixed */ public function asListener(InstructionCategoryActionEvent $event) { if ($this->isAllowed($event)) { return $this->handle($event); } } /** * Check if the event action is allowed. * * @param InstructionCategoryActionEvent $event * @return bool */ private function isAllowed(InstructionCategoryActionEvent $event) { if ($event->when == 'before' && !in_array($event->action, $this->allowedActions['before'])) { return false; } if (($event->when == 'after' && !in_array($event->action, $this->allowedActions['after'])) ) { return false; } return true; } }