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