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