Passed
Pull Request — master (#10)
by Anatoly
02:53
created

ErrorHandlingMiddleware::handleRouteNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 5
nc 1
nop 2
dl 0
loc 9
c 3
b 0
f 0
cc 1
rs 10
ccs 0
cts 7
cp 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace App\Middleware;
4
5
/**
6
 * Import classes
7
 */
8
use App\ContainerAwareTrait;
9
use Arus\Http\Response\ResponseFactoryAwareTrait;
10
use Middlewares\Utils\HttpErrorException as InvalidPayloadException;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\MiddlewareInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use Sunrise\Http\Router\Exception\BadRequestException;
16
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
17
use Sunrise\Http\Router\Exception\PageNotFoundException;
18
use Sunrise\Http\Router\Exception\UnsupportedMediaTypeException;
19
use Throwable;
20
21
/**
22
 * ErrorHandlingMiddleware
23
 */
24
final class ErrorHandlingMiddleware implements MiddlewareInterface
25
{
26
    use ContainerAwareTrait;
27
    use ResponseFactoryAwareTrait;
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @param ServerRequestInterface $request
33
     * @param RequestHandlerInterface $handler
34
     *
35
     * @return ResponseInterface
36
     */
37
    public function process(
38
        ServerRequestInterface $request,
39
        RequestHandlerInterface $handler
40
    ) : ResponseInterface {
41
        try {
42
            return $handler->handle($request);
43
        } catch (InvalidPayloadException $e) {
44
            return $this->handleInvalidPayload($request, $e);
45
        } catch (BadRequestException $e) {
46
            return $this->handleBadRequest($request, $e);
47
        } catch (PageNotFoundException $e) {
48
            return $this->handlePageNotFound($request, $e);
49
        } catch (MethodNotAllowedException $e) {
50
            return $this->handleMethodNotAllowed($request, $e);
51
        } catch (UnsupportedMediaTypeException $e) {
52
            return $this->handleUnsupportedMediaType($request, $e);
53
        } catch (Throwable $e) {
54
            return $this->handleUnexpectedError($request, $e);
55
        }
56
    }
57
58
    /**
59
     * @param ServerRequestInterface $request
60
     * @param InvalidPayloadException $e
61
     *
62
     * @return ResponseInterface
63
     */
64
    private function handleInvalidPayload(
65
        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

65
        /** @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...
66
        InvalidPayloadException $e
0 ignored issues
show
Unused Code introduced by
The parameter $e 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

66
        /** @scrutinizer ignore-unused */ InvalidPayloadException $e

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...
67
    ) : ResponseInterface {
68
        return $this->error('Invalid payload', 'requestBody', null, 400);
69
    }
70
71
    /**
72
     * @param ServerRequestInterface $request
73
     * @param BadRequestException $e
74
     *
75
     * @return ResponseInterface
76
     */
77
    private function handleBadRequest(
78
        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

78
        /** @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...
79
        BadRequestException $e
80
    ) : ResponseInterface {
81
        return $this->violations($e->getViolations(), 400);
82
    }
83
84
    /**
85
     * @param ServerRequestInterface $request
86
     * @param PageNotFoundException $e
87
     *
88
     * @return ResponseInterface
89
     */
90
    private function handlePageNotFound(
91
        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

91
        /** @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...
92
        PageNotFoundException $e
0 ignored issues
show
Unused Code introduced by
The parameter $e 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

92
        /** @scrutinizer ignore-unused */ PageNotFoundException $e

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...
93
    ) : ResponseInterface {
94
        return $this->createResponse(404);
95
    }
96
97
    /**
98
     * @param ServerRequestInterface $request
99
     * @param MethodNotAllowedException $e
100
     *
101
     * @return ResponseInterface
102
     */
103
    private function handleMethodNotAllowed(
104
        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

104
        /** @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...
105
        MethodNotAllowedException $e
106
    ) : ResponseInterface {
107
        return $this->createResponse(405)
108
            ->withHeader('Allow', $e->getJoinedAllowedMethods());
109
    }
110
111
    /**
112
     * @param ServerRequestInterface $request
113
     * @param UnsupportedMediaTypeException $e
114
     *
115
     * @return ResponseInterface
116
     */
117
    private function handleUnsupportedMediaType(
118
        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

118
        /** @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...
119
        UnsupportedMediaTypeException $e
120
    ) : ResponseInterface {
121
        return $this->createResponse(415)
122
            ->withHeader('Accept', $e->getJoinedSupportedTypes());
123
    }
124
125
    /**
126
     * @param ServerRequestInterface $request
127
     * @param Throwable $e
128
     *
129
     * @return ResponseInterface
130
     */
131
    private function handleUnexpectedError(
132
        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

132
        /** @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...
133
        Throwable $e
134
    ) : ResponseInterface {
135
        $this->container->get('logger')->error($e->getMessage(), [
136
            'exception' => $e,
137
        ]);
138
139
        if ($this->container->get('app.silent')) {
140
            return $this->createResponse(500);
141
        }
142
143
        return $this->error(
144
            $e->getMessage(),
145
            $e->getFile() .':'. $e->getLine(),
146
            $e->getCode(),
147
            500
148
        );
149
    }
150
}
151