ExceptionRouter::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
namespace Mouf\Mvc\Splash\Routers;
4
5
use Mouf\Mvc\Splash\Controllers\Http500HandlerInterface;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
use Psr\Log\LoggerInterface;
10
use Mouf\Mvc\Splash\Services\SplashUtils;
11
use Zend\Stratigility\ErrorMiddlewareInterface;
12
13
/**
14
 * This router transforms exceptions into HTTP 500 pages, based on the configured error controller.
15
 *
16
 * @author Kevin Nguyen
17
 * @author David Négrier
18
 */
19
class ExceptionRouter implements ErrorMiddlewareInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\Stratigility\ErrorMiddlewareInterface has been deprecated with message: since 1.3.0; will be removed with 2.0.0. Please see https://docs.zendframework.com/zend-stratigility/migration/to-v2/ for more information on how to update your code for forwards compatibility.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    /**
22
     * The logger.
23
     *
24
     * @var LoggerInterface
25
     */
26
    private $log;
27
28
    /**
29
     * The controller that will display 500 errors.
30
     *
31
     * @var Http500HandlerInterface
32
     */
33
    private $errorController;
34
35
    /**
36
     * @Important
37
     *
38
     * @param HttpKernelInterface $router The default router (the router we will catch exceptions from).
0 ignored issues
show
Bug introduced by
There is no parameter named $router. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @param LoggerInterface     $log    Logger to log errors.
40
     */
41
    public function __construct(Http500HandlerInterface $errorController, LoggerInterface $log = null)
42
    {
43
        $this->errorController = $errorController;
44
        $this->log = $log;
45
    }
46
47
    /**
48
     * Actually handle the exception.
49
     *
50
     * @param \Exception $e
51
     *
52
     * @return ResponseInterface
53
     */
54
    private function handleException(\Exception $e, Request $request)
55
    {
56
        if ($this->log != null) {
57
            $this->log->error('Exception thrown inside a controller.', array(
58
                    'exception' => $e,
59
            ));
60
        } else {
61
            // If no logger is set, let's log in PHP error_log
62
            error_log($e->getMessage().' - '.$e->getTraceAsString());
63
        }
64
65
        $response = SplashUtils::buildControllerResponse(
66
            function () use ($e, $request) {
67
                return $this->errorController->serverError($e, $request);
68
            }
69
        );
70
71
        return $response;
72
    }
73
74
    /**
75
     * Process an incoming error, along with associated request and response.
76
     *
77
     * Accepts an error, a server-side request, and a response instance, and
78
     * does something with them; if further processing can be done, it can
79
     * delegate to `$out`.
80
     *
81
     * @see MiddlewareInterface
82
     *
83
     * @param mixed         $error
84
     * @param Request       $request
85
     * @param Response      $response
86
     * @param null|callable $out
87
     *
88
     * @return null|Response
89
     */
90
    public function __invoke($error, Request $request, Response $response, callable $out = null)
91
    {
92
        return $this->handleException($error, $request);
93
    }
94
}
95