Test Failed
Push — master ( fdaa2f...711c23 )
by Derek Stephen
09:23
created

ExceptionMiddleware::process()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 9.5555
cc 5
nc 9
nop 2
1
<?php
2
3
namespace Bone\View\Middleware;
4
5
use Bone\View\ViewEngineInterface;
6
use Exception;
7
use Laminas\Diactoros\Response\HtmlResponse;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response\HtmlResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 = $e->getStatusCode();
0 ignored issues
show
Bug introduced by
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
            /** @scrutinizer ignore-call */ 
42
            $code = $e->getStatusCode();

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
                'trace' => $e->getTrace(),
49
            ]);
50
51
            $response = new HtmlResponse($body, $status);
52
        }
53
54
        return $response;
55
    }
56
}