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