| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Controllers; |
| 4: | |
| 5: | use Exception; |
| 6: | use Illuminate\Contracts\Container\BindingResolutionException; |
| 7: | use Illuminate\Contracts\Container\CircularDependencyException; |
| 8: | use Illuminate\Http\JsonResponse; |
| 9: | use LaravelUi5\Core\Contracts\Ui5Context; |
| 10: | use LaravelUi5\Core\Exceptions\InvalidReportActionException; |
| 11: | use LaravelUi5\Core\Exceptions\MissingReportActionException; |
| 12: | use LaravelUi5\Core\Services\ExecutableHandler; |
| 13: | use LaravelUi5\Core\Ui5\Contracts\ReportActionInterface; |
| 14: | use LaravelUi5\Core\Ui5\Contracts\Ui5ReportInterface; |
| 15: | |
| 16: | class ReportActionDispatchController |
| 17: | { |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | public function __invoke( |
| 23: | Ui5Context $context, |
| 24: | ExecutableHandler $handler, |
| 25: | string $slug, |
| 26: | string $action |
| 27: | ): JsonResponse |
| 28: | { |
| 29: | |
| 30: | $report = $context->artifact; |
| 31: | |
| 32: | $actions = $report->getActions(); |
| 33: | if(!array_key_exists($action, $actions)) { |
| 34: | throw new MissingReportActionException($action, $report->getTitle()); |
| 35: | } |
| 36: | |
| 37: | $instance = app($actions[$action]); |
| 38: | |
| 39: | if (!$instance instanceof ReportActionInterface) { |
| 40: | throw new InvalidReportActionException($action); |
| 41: | } |
| 42: | |
| 43: | try { |
| 44: | $result = $handler->run($instance); |
| 45: | |
| 46: | return response()->json([ |
| 47: | 'status' => 'Success', |
| 48: | 'code' => 200, |
| 49: | 'data' => $result, |
| 50: | ]); |
| 51: | } |
| 52: | catch (Exception $e) { |
| 53: | return response()->json([ |
| 54: | 'status' => 'Error', |
| 55: | 'code' => 500, |
| 56: | 'message' => $e->getMessage(), |
| 57: | 'errors' => method_exists($e, 'errors') ? $e->errors() : null, |
| 58: | ]); |
| 59: | } |
| 60: | } |
| 61: | } |
| 62: | |