Failed Conditions
Push — master ( 1ef376...9977a5 )
by Arnold
03:50
created

Middleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 65
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 24 5
A errorResponse() 0 7 1
1
<?php
2
3
namespace Jasny\ErrorHandler;
4
5
use Jasny\ErrorHandler;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Use error handler as middleware
11
 */
12
class Middleware
13
{
14
    /**
15
     * @var ErrorHandler
16
     */
17
    protected $errorHandler;
18
    
19
    /**
20
     * Class constructor
21
     * 
22
     * @param ErrorHandler $errorHandler
23
     */
24 9
    public function __construct(ErrorHandler $errorHandler)
25
    {
26 9
        $this->errorHandler = $errorHandler;
27 9
    }
28
    
29
    /**
30
     * Run middleware action
31
     *
32
     * @param ServerRequestInterface $request
33
     * @param ResponseInterface      $response
34
     * @param callback               $next
35
     * @return ResponseInterface
36
     */
37 9
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
38
    {
39 9
        if (!is_callable($next)) {
40 2
            throw new \InvalidArgumentException("'next' should be a callback");            
41
        }
42
43
        try {
44 7
            $nextResponse = $next($request, $response);
45 2
            $error = null;
46 6
        } catch (\Error $e) {
1 ignored issue
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47 1
            $error = $e;
48 4
        } catch (\Exception $e) {
49 4
            $error = $e;
50
        }
51
        
52 7
        $this->errorHandler->setError($error);
53
        
54 7
        if ($error) {
55 5
            $this->errorHandler->log($error);
56 5
            $nextResponse = $this->errorResponse($request, $response);
57 2
        }
58
        
59 7
        return $nextResponse;
60
    }
61
62
    /**
63
     * Handle caught error
64
     *
65
     * @param ServerRequestInterface $request
66
     * @param ResponseInterface      $response
67
     * @return ResponseInterface
68
     */
69 5
    protected function errorResponse(ServerRequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
70
    {
71 5
        $errorResponse = $response->withStatus(500);
72 5
        $errorResponse->getBody()->write('Unexpected error');
73
74 5
        return $errorResponse;
75
    }
76
}
77