From f3d2cf5e153024e452ea2cf4ae873e28f5583ca6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 26 Jul 2016 19:10:43 +0200 Subject: [PATCH] Deleted ResponseTypeMiddleware which is not ussable anymore --- module/Rest/config/services.config.php | 1 - .../src/Middleware/ResponseTypeMiddleware.php | 99 ------------------- 2 files changed, 100 deletions(-) delete mode 100644 module/Rest/src/Middleware/ResponseTypeMiddleware.php diff --git a/module/Rest/config/services.config.php b/module/Rest/config/services.config.php index 05a1afbe..b2c77bc7 100644 --- a/module/Rest/config/services.config.php +++ b/module/Rest/config/services.config.php @@ -19,7 +19,6 @@ return [ Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class, - Middleware\ResponseTypeMiddleware::class => AnnotatedFactory::class, Middleware\NotFoundMiddleware::class => AnnotatedFactory::class, ], ], diff --git a/module/Rest/src/Middleware/ResponseTypeMiddleware.php b/module/Rest/src/Middleware/ResponseTypeMiddleware.php deleted file mode 100644 index e32bef12..00000000 --- a/module/Rest/src/Middleware/ResponseTypeMiddleware.php +++ /dev/null @@ -1,99 +0,0 @@ -translator = $translator; - } - - /** - * Process an incoming error, along with associated request and response. - * - * Accepts an error, a server-side request, and a response instance, and - * does something with them; if further processing can be done, it can - * delegate to `$out`. - * - * @see MiddlewareInterface - * @param Request $request - * @param Response $response - * @param null|callable $out - * @return null|Response - */ - public function __invoke(Request $request, Response $response, callable $out = null) - { - $accept = $request->getHeader('Accept'); - if (! empty(array_intersect(['application/json', 'text/json', 'application/x-json'], $accept))) { - $status = $this->determineStatus($request, $response); - $errorData = $this->determineErrorCode($request, $status); - - return new JsonResponse($errorData, $status); - } - - return $out($request, $response); - } - - /** - * @param Request $request - * @param Response $response - * @return int - */ - protected function determineStatus(Request $request, Response $response) - { - /** @var RouteResult $routeResult */ - $routeResult = $request->getAttribute(RouteResult::class); - if ($routeResult->isFailure()) { - return 404; - } - - $status = $response->getStatusCode(); - return $status >= 400 ? $status : 500; - } - - /** - * @param Request $request - * @param int $status - * @return string - */ - protected function determineErrorCode(Request $request, $status) - { - $errorData = $request->getAttribute('errorData'); - if (isset($errorData)) { - return $errorData; - } - - switch ($status) { - case 404: - return [ - 'error' => RestUtils::NOT_FOUND_ERROR, - 'message' => $this->translator->translate('Requested route does not exist'), - ]; - default: - return [ - 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => $this->translator->translate('Unknown error occured'), - ]; - } - } -}