Issues (2)

src/Middleware/ExceptionMiddleware.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Bone\View\Middleware;
4
5
use Bone\View\ViewEngineInterface;
6
use Exception;
7
use Laminas\Diactoros\Response\HtmlResponse;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
class ExceptionMiddleware extends AbstractViewMiddleware implements MiddlewareInterface
14
{
15
    /** @var ViewEngineInterface $viewEngine */
16
    private $viewEngine;
17
18
    /** @var array $errorPages\ */
19
    private $errorPages;
20
21
    /**
22
     * LayoutMiddleware constructor.
23
     * @param ViewEngineInterface $viewEngine
24
     */
25
    public function __construct(ViewEngineInterface $viewEngine, array $errorPages)
26
    {
27
        $this->viewEngine = $viewEngine;
28
        $this->errorPages = $errorPages;
29
    }
30
31
    /**
32
     * @param ServerRequestInterface $request
33
     * @param RequestHandlerInterface $handler
34
     * @return ResponseInterface
35
     */
36
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
37
    {
38
        try {
39
            $response = $handler->handle($request);
40
        } catch (Exception $e) {
41
            $code = strstr(get_class($e), 'League') ? $e->getStatusCode() : $e->getCode();
0 ignored issues
show
The method getStatusCode() does not exist on Exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $code = strstr(get_class($e), 'League') ? $e->/** @scrutinizer ignore-call */ getStatusCode() : $e->getCode();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
            $status = ($code >= 100 && $code < 600) ? $code : 500;
43
            $layout = array_key_exists($status, $this->errorPages) ? $this->errorPages[$status] : $this->errorPages['exception'];
44
45
            $body = $this->viewEngine->render($layout, [
46
                'message' => $e->getMessage(),
47
                'code' => $status,
48
                'line' => $e->getLine(),
49
                'file' => $e->getFile(),
50
            ]);
51
52
            $response = new HtmlResponse($body, $status);
53
        }
54
55
        return $response;
56
    }
57
}
58