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