Completed
Push — 8.2 ( 737f16...22bfc7 )
by David
17:32
created

ExceptionRouter::__invoke()   A

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 Interop\Http\ServerMiddleware\DelegateInterface;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7
use Mouf\Mvc\Splash\Controllers\Http500HandlerInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Log\LoggerInterface;
12
use Mouf\Mvc\Splash\Services\SplashUtils;
13
14
/**
15
 * This router transforms exceptions into HTTP 500 pages, based on the configured error controller.
16
 *
17
 * @author Kevin Nguyen
18
 * @author David Négrier
19
 */
20
class ExceptionRouter implements MiddlewareInterface
21
{
22
    /**
23
     * The logger.
24
     *
25
     * @var LoggerInterface
26
     */
27
    private $log;
28
29
    /**
30
     * The controller that will display 500 errors.
31
     *
32
     * @var Http500HandlerInterface
33
     */
34
    private $errorController;
35
36
    /**
37
     * @Important
38
     *
39
     * @param Http500HandlerInterface $errorController The controller in charge of displaying the HTTP 500 error.
40
     * @param LoggerInterface $log Logger to log errors.
41
     */
42
    public function __construct(Http500HandlerInterface $errorController, LoggerInterface $log = null)
43
    {
44
        $this->errorController = $errorController;
45
        $this->log = $log;
46
    }
47
48
    /**
49
     * Actually handle the exception.
50
     *
51
     * @param \Throwable $t
52
     * @param Request $request
53
     * @return ResponseInterface
54
     */
55
    private function handleException(\Throwable $t, Request $request)
56
    {
57
        if ($this->log !== null) {
58
            $this->log->error('Exception thrown inside a controller.', array(
59
                    'exception' => $t,
60
            ));
61
        } else {
62
            // If no logger is set, let's log in PHP error_log
63
            error_log($t->getMessage().' - '.$t->getTraceAsString());
64
        }
65
66
        $response = SplashUtils::buildControllerResponse(
67
            function () use ($t, $request) {
68
                return $this->errorController->serverError($t, $request);
69
            }
70
        );
71
72
        return $response;
73
    }
74
75
    /**
76
     * Process an incoming server request and return a response, optionally delegating
77
     * to the next middleware component to create the response.
78
     *
79
     * @param Request $request
80
     * @param DelegateInterface $delegate
81
     *
82
     * @return Response
83
     */
84
    public function process(Request $request, DelegateInterface $delegate)
85
    {
86
        try {
87
            return $delegate->process($request);
88
        } catch (\Throwable $t) {
89
            return $this->handleException($t, $request);
90
        }
91
    }
92
}
93