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