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