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