Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

RendererMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 10 2
A getContent() 0 9 1
A writeContent() 0 6 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Http;
11
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Slick\Http\Server\AbstractMiddleware;
15
use Slick\Http\Server\MiddlewareInterface;
16
use Slick\Http\Stream;
17
use Slick\Mvc\Http\Renderer\ViewInflectorInterface;
18
use Slick\Template\TemplateEngineInterface;
19
20
/**
21
 * Renderer Middleware
22
 *
23
 * @package Slick\Mvc\Http
24
 * @author  Filipe Silva <[email protected]>
25
 */
26
final class RendererMiddleware extends AbstractMiddleware implements
27
    MiddlewareInterface
28
{
29
    /**
30
     * @var TemplateEngineInterface
31
     */
32
    private $templateEngine;
33
34
    /**
35
     * @var ViewInflectorInterface
36
     */
37
    private $inflector;
38
39
    /**
40
     * Creates a renderer middleware
41
     *
42
     * @param TemplateEngineInterface $templateEngine
43
     * @param ViewInflectorInterface $inflector
44
     */
45
    public function __construct(
46
        TemplateEngineInterface $templateEngine,
47
        ViewInflectorInterface $inflector
48
    ) {
49
        $this->templateEngine = $templateEngine;
50
        $this->inflector = $inflector;
51
    }
52
53
    /**
54
     * Handles a Request and updated the response
55
     *
56
     * @param ServerRequestInterface $request
57
     * @param ResponseInterface $response
58
     *
59
     * @return ResponseInterface
60
     */
61
    public function handle(
62
        ServerRequestInterface $request, ResponseInterface $response
63
    ) {
64
        if ($response->getStatusCode() !== 302) {
65
            $content = $this->getContent($request);
66
            $response = $this->writeContent($response, $content);
67
        }
68
69
        return $this->executeNext($request, $response);
70
    }
71
72
    /**
73
     * Get content from template processing
74
     *
75
     * @param ServerRequestInterface $request
76
     *
77
     * @return string
78
     */
79
    private function getContent(ServerRequestInterface $request)
80
    {
81
        $templateFile = $this->inflector
82
            ->inflect($request->getAttribute('route'));
83
        $data = $request->getAttribute('viewData', []);
84
        return $this->templateEngine
85
            ->parse($templateFile)
86
            ->process($data);
87
    }
88
89
    /**
90
     * Write content to the resulting response
91
     *
92
     * @param ResponseInterface $response
93
     * @param string            $content
94
     *
95
     * @return ResponseInterface
96
     */
97
    private function writeContent(ResponseInterface $response, $content)
98
    {
99
        $body = new Stream('php://memory', 'rw+');
100
        $body->write($content);
101
        return $response->withBody($body);
102
    }
103
}