ExceptionMiddleware::process()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.6111
cc 5
nc 5
nop 2
crap 5
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
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
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
17
class ExceptionMiddleware implements MiddlewareInterface
18
{
19
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $handler should have a doc-comment as per coding-style.
Loading history...
20
     * {@inheritDoc}
21
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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