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