Completed
Push — master ( 753e69...36642f )
by Derek Stephen
02:06 queued 13s
created

ExceptionDecorator::getResponseWithBodyAndStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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), $body, $status);
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
}