<?php namespace Eform\Faq\Listeners; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Str; use Eform\Faq\Events\FaqAction as FaqActionEvent; use Eform\Faq\Notifications\FaqAction as FaqActionNotification; use Litepie\Actions\Concerns\AsAction; class FaqAction { use AsAction; private $allowedActions = [ 'before' => [], 'after' => ['create'], ]; /** * Handle the FaqActionEvent. * * @param FaqActionEvent $event * @return mixed */ public function handle(FaqActionEvent $event) { $function = Str::camel($event->action); return $this->$function($event); } /** * Create a new $faq. * * @param FaqActionEvent $event * @return void */ public function create(FaqActionEvent $event) { $client = $event->faq->client; Notification::send($client, new FaqActionNotification($event)); } /** * Handle the FaqActionEvent as a listener. * * @param FaqActionEvent $event * @return mixed */ public function asListener(FaqActionEvent $event) { if ($this->isAllowed($event)) { return $this->handle($event); } } /** * Check if the event action is allowed. * * @param FaqActionEvent $event * @return bool */ private function isAllowed(FaqActionEvent $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; } }