Failed Conditions
Push — master ( 8a3267...5d79a7 )
by Arnold
03:19
created

Middleware::__invoke()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.5125
cc 5
eloc 15
nc 7
nop 3
crap 5
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
use Jasny\HttpMessage\Response as JasnyResponse;
9
10
/**
11
 * Use error handler as middleware
12
 */
13
class Middleware
14
{
15
    /**
16
     * @var ErrorHandler
17
     */
18
    protected $errorHandler;
19
    
20
    /**
21
     * Class constructor
22
     * 
23
     * @param ErrorHandler $errorHandler
24
     */
25 9
    public function __construct(ErrorHandler $errorHandler)
26 1
    {
27 9
        $this->errorHandler = $errorHandler;
28 9
    }
29
    
30
    /**
31
     * Run middleware action
32
     *
33
     * @param ServerRequestInterface $request
34
     * @param ResponseInterface      $response
35
     * @param callback               $next
36
     * @return ResponseInterface
37
     */
38 9
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
39
    {
40 9
        if (!is_callable($next)) {
41 2
            throw new \InvalidArgumentException("'next' should be a callback");            
42
        }
43
44
        try {
45 7
            $nextResponse = $next($request, $response);
46 2
            $error = null;
47 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...
48 1
            $error = $e;
49 4
        } catch (\Exception $e) {
50 4
            $error = $e;
51
        }
52
        
53 7
        $this->errorHandler->setError($error);
54
        
55 7
        if ($error) {
56 5
            $this->errorHandler->log($error);
57 5
            $nextResponse = $this->errorResponse($request, $response);
58 1
        }
59
        
60 4
        return $nextResponse;
61
    }
62
63
    /**
64
     * Handle caught error
65
     *
66
     * @param ServerRequestInterface $request
67
     * @param ResponseInterface      $response
68
     * @return ResponseInterface
69
     */
70 5
    protected function errorResponse(ServerRequestInterface $request, ResponseInterface $response)
71
    {
72 5
        if ($response instanceof JasnyResponse && $response->isStale()) {
0 ignored issues
show
Bug introduced by
The class Jasny\HttpMessage\Response does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
            $response = $response->revive();
74
        }
75
76 5
        $errorResponse = $response->withProtocolVersion($request->getProtocolVersion())->withStatus(500);
77 5
        $errorResponse->getBody()->write('An unexpected error occured');
78
79 2
        return $errorResponse;
80
    }
81
}
82
83