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