Completed
Push — dev-master ( 3d2c20...b7e24f )
by Derek Stephen
08:18 queued 03:13
created

NotFoundDecorator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setView() 0 4 1
A process() 0 13 1
1
<?php
2
3
namespace Bone\Mvc\Router\Decorator;
4
5
use Bone\Mvc\View\ViewEngine;
6
use Bone\Traits\LayoutAwareTrait;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\Stream;
13
14
class NotFoundDecorator implements MiddlewareInterface
15
{
16
    use LayoutAwareTrait;
17
18
19
    /** @var ViewEngine  */
20
    private $viewEngine;
21
22
    /** @var string $view */
23
    private $view;
24
25
    /**
26
     * NotFoundDecorator constructor.
27
     * @param ViewEngine $viewEngine
28
     */
29
    public function __construct(ViewEngine $viewEngine)
30
    {
31
        $this->viewEngine = $viewEngine;
32
        $this->view = 'error/not-found';
33
    }
34
35
    /**
36
     * @param string $view
37
     */
38
    protected function setView(string $view)
39
    {
40
        $this->view = $view;
41
    }
42
43
    /**
44
     * @param ServerRequestInterface $request
45
     * @param RequestHandlerInterface $handler
46
     * @return ResponseInterface
47
     */
48
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49
    {
50
        $body = $this->viewEngine->render($this->view);
51
        $body = $this->viewEngine->render($this->getLayout(), [
52
            'content' => $body,
53
        ]);
54
55
        $stream = new Stream('php://memory', 'r+');
56
        $stream->write($body);
57
        $response = (new Response())->withStatus(404)->withBody($stream);
58
59
        return $response;
60
    }
61
}