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