Passed
Push — master ( 03e013...bd5966 )
by Anatoly
01:18 queued 14s
created

ErrorHandlingMiddleware::process()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
nc 6
nop 2
dl 0
loc 16
c 2
b 0
f 0
cc 6
ccs 0
cts 16
cp 0
crap 42
rs 9.2222
1
<?php declare(strict_types=1);
2
3
namespace App\Middleware;
4
5
/**
6
 * Import classes
7
 */
8
use App\ContainerAwareTrait;
9
use App\Http\ResponseFactory;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
use Sunrise\Http\Router\Exception\BadRequestException;
15
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
16
use Sunrise\Http\Router\Exception\RouteNotFoundException;
17
use Sunrise\Http\Router\Exception\UnsupportedMediaTypeException;
18
use Whoops\Run as Whoops;
19
use Whoops\Handler\PrettyPageHandler;
20
use Throwable;
21
22
/**
23
 * Import functions
24
 */
25
use function implode;
26
27
/**
28
 * ErrorHandlingMiddleware
29
 */
30
final class ErrorHandlingMiddleware implements MiddlewareInterface
31
{
32
    use ContainerAwareTrait;
33
34
    /**
35
     * {@inheritDoc}
36
     *
37
     * @param ServerRequestInterface $request
38
     * @param RequestHandlerInterface $handler
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function process(
43
        ServerRequestInterface $request,
44
        RequestHandlerInterface $handler
45
    ) : ResponseInterface {
46
        try {
47
            return $handler->handle($request);
48
        } catch (BadRequestException $e) {
49
            return $this->handleBadRequest($request, $e);
50
        } catch (MethodNotAllowedException $e) {
51
            return $this->handleMethodNotAllowed($request, $e);
52
        } catch (RouteNotFoundException $e) {
53
            return $this->handleRouteNotFound($request, $e);
54
        } catch (UnsupportedMediaTypeException $e) {
55
            return $this->handleUnsupportedMediaType($request, $e);
56
        } catch (Throwable $e) {
57
            return $this->handleException($request, $e);
58
        }
59
    }
60
61
    /**
62
     * Returns a response with the given processed exception
63
     *
64
     * @param ServerRequestInterface $request
65
     * @param BadRequestException $exception
66
     *
67
     * @return ResponseInterface
68
     */
69
    private function handleBadRequest(
70
        ServerRequestInterface $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
        /** @scrutinizer ignore-unused */ ServerRequestInterface $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
        BadRequestException $exception
72
    ) : ResponseInterface {
73
        return (new ResponseFactory)->error($exception->getMessage(), $exception->getViolations(), 400);
74
    }
75
76
    /**
77
     * Returns a response with the given processed exception
78
     *
79
     * @param ServerRequestInterface $request
80
     * @param MethodNotAllowedException $exception
81
     *
82
     * @return ResponseInterface
83
     */
84
    private function handleMethodNotAllowed(
85
        ServerRequestInterface $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
        /** @scrutinizer ignore-unused */ ServerRequestInterface $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
        MethodNotAllowedException $exception
87
    ) : ResponseInterface {
88
        return (new ResponseFactory)->error($exception->getMessage(), [], 405)
89
            ->withHeader('Allow', implode(',', $exception->getAllowedMethods()));
90
    }
91
92
    /**
93
     * Returns a response with the given processed exception
94
     *
95
     * @param ServerRequestInterface $request
96
     * @param RouteNotFoundException $exception
97
     *
98
     * @return ResponseInterface
99
     */
100
    private function handleRouteNotFound(
101
        ServerRequestInterface $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
        /** @scrutinizer ignore-unused */ ServerRequestInterface $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
        RouteNotFoundException $exception
103
    ) : ResponseInterface {
104
        return (new ResponseFactory)->error($exception->getMessage(), [], 404);
105
    }
106
107
    /**
108
     * Returns a response with the given processed exception
109
     *
110
     * @param ServerRequestInterface $request
111
     * @param UnsupportedMediaTypeException $exception
112
     *
113
     * @return ResponseInterface
114
     */
115
    private function handleUnsupportedMediaType(
116
        ServerRequestInterface $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
        /** @scrutinizer ignore-unused */ ServerRequestInterface $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
        UnsupportedMediaTypeException $exception
118
    ) : ResponseInterface {
119
        return (new ResponseFactory)->error($exception->getMessage(), [], 415)
120
            ->withHeader('Accept', implode(',', $exception->getSupportedTypes()));
121
    }
122
123
    /**
124
     * Returns a response with the given processed exception
125
     *
126
     * @param ServerRequestInterface $request
127
     * @param Throwable $exception
128
     *
129
     * @return ResponseInterface
130
     *
131
     * @link https://github.com/filp/whoops
132
     */
133
    private function handleException(
134
        ServerRequestInterface $request,
135
        Throwable $exception
136
    ) : ResponseInterface {
137
        $this->container->get('logger')->error($exception->getMessage(), [
138
            'exception' => $exception,
139
        ]);
140
141
        if (!$this->container->get('app.display_errors')) {
142
            return (new ResponseFactory)->createResponse(500);
143
        }
144
145
        $whoops = new Whoops();
146
        $whoops->allowQuit(false);
147
        $whoops->sendHttpCode(false);
148
        $whoops->writeToOutput(false);
149
        $whoops->pushHandler(new PrettyPageHandler());
150
151
        return (new ResponseFactory)->html($whoops->handleException($exception), 500);
152
    }
153
}
154