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