dmt-software /
insolvency-client
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace DMT\Insolvency\Http\Middleware; |
||
| 4 | |||
| 5 | use DMT\Http\Client\MiddlewareInterface; |
||
| 6 | use DMT\Http\Client\RequestHandlerInterface; |
||
| 7 | use DMT\Insolvency\Exception\AuthorizationException; |
||
| 8 | use DMT\Insolvency\Exception\NotFoundException; |
||
| 9 | use DMT\Insolvency\Exception\ResponseException; |
||
| 10 | use DMT\Insolvency\Exception\UnavailableException; |
||
| 11 | use Psr\Http\Message\RequestInterface; |
||
| 12 | use Psr\Http\Message\ResponseInterface; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class ExceptionMiddleware |
||
| 16 | */ |
||
|
0 ignored issues
–
show
|
|||
| 17 | class ExceptionMiddleware implements MiddlewareInterface |
||
| 18 | { |
||
| 19 | /** |
||
|
0 ignored issues
–
show
|
|||
| 20 | * {@inheritDoc} |
||
| 21 | */ |
||
|
0 ignored issues
–
show
|
|||
| 22 | 17 | public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
| 23 | { |
||
| 24 | 17 | $response = $handler->handle($request); |
|
| 25 | |||
| 26 | 17 | if ($response->getStatusCode() >= 500) { |
|
| 27 | 2 | throw new UnavailableException($response->getReasonPhrase()); |
|
| 28 | } |
||
| 29 | |||
| 30 | 15 | if ($response->getStatusCode() === 404) { |
|
| 31 | 1 | throw new NotFoundException('No results found'); |
|
| 32 | } |
||
| 33 | |||
| 34 | 14 | if ($response->getStatusCode() === 401) { |
|
| 35 | 1 | throw new AuthorizationException($response->getReasonPhrase()); |
|
| 36 | } |
||
| 37 | |||
| 38 | 13 | if ($response->getStatusCode() >= 400) { |
|
| 39 | 1 | throw new ResponseException($response->getReasonPhrase()); |
|
| 40 | } |
||
| 41 | |||
| 42 | 12 | return $response; |
|
| 43 | } |
||
| 44 | } |
||
| 45 |