Completed
Push — master ( 7772bc...b7c027 )
by Derek Stephen
01:36
created

src/Mvc/Router/Decorator/ExceptionDecorator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Bone\Mvc\Router\Decorator;
4
5
use Bone\Mvc\View\ViewEngine;
6
use Bone\Traits\HasLayoutTrait;
7
use Exception;
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
use Zend\Diactoros\Response\HtmlResponse;
13
use Zend\Diactoros\Stream;
14
15
class ExceptionDecorator implements MiddlewareInterface
16
{
17
18
    use HasLayoutTrait;
19
20
    /** @var ViewEngine  */
21
    protected $viewEngine;
22
23
    /** @var string $view */
24
    protected $view;
25
26
    /**
27
     * @var array $templates
28
     */
29
    protected $templates;
30
31
    /**
32
     * ExceptionDecorator constructor.
33
     * @param ViewEngine $viewEngine
34
     * @param array $templates
35
     */
36
    public function __construct(ViewEngine $viewEngine, array $templates)
37
    {
38
        $this->viewEngine = $viewEngine;
39
        $this->templates = $templates;
40
        $this->view = 'error/error';
41
    }
42
43
    /**
44
     * @param string $view
45
     */
46
    protected function setView(string $view)
47
    {
48
        $this->view = $view;
49
    }
50
51
    /**
52
     * @param ServerRequestInterface $request
53
     * @param RequestHandlerInterface $handler
54
     * @return ResponseInterface
55
     */
56
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57
    {
58
        try {
59
            return $handler->handle($request);
60
        } catch (Exception $e) {
61
62
            $template = 'error/error';
63
            $code = $e->getCode();
64
65
            if (array_key_exists($code, $this->templates)) {
66
                $template = $this->templates[$code];
67
            } elseif (array_key_exists('exception', $this->templates)) {
68
                $template = $this->templates['exception'];
69
            }
70
71
            $body = $this->viewEngine->render($template, [
72
                'message' => $e->getMessage(),
73
                'code' => $e->getCode(),
74
                'trace' => $e->getTrace(),
75
            ]);
76
            $body = $this->viewEngine->render($this->layout, [
77
                'content' => $body,
78
            ]);
79
            $status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500;
80
81
            return $this->getResponseWithBodyAndStatus(new HtmlResponse(), $body, $status);
0 ignored issues
show
The call to HtmlResponse::__construct() misses a required argument $html.

This check looks for function calls that miss required arguments.

Loading history...
82
        }
83
    }
84
85
86
87
    /**
88
     * @param ResponseInterface $response
89
     * @param string $body
90
     * @param int $status
91
     * @return \Psr\Http\Message\MessageInterface|Response
92
     */
93 View Code Duplication
    protected function getResponseWithBodyAndStatus(ResponseInterface $response, string $body, int $status = 200)
94
    {
95
        $stream = new Stream('php://memory', 'r+');
96
        $stream->write($body);
97
        $response = $response->withStatus($status)->withBody($stream);
98
99
        return $response;
100
    }
101
}