Completed
Push — master ( ef610a...dc0762 )
by Gabor
24:45
created

DispatcherMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Middleware;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Message\StreamInterface;
17
use WebHemi\Adapter\Renderer\RendererAdapterInterface;
18
19
/**
20
 * Class DispatcherMiddleware.
21
 */
22
class DispatcherMiddleware implements MiddlewareInterface
23
{
24
    /** @var RendererAdapterInterface */
25
    private $templateRenderer;
26
27
    /**
28
     * DispatcherMiddleware constructor.
29
     *
30
     * @param RendererAdapterInterface $templateRenderer
31
     */
32
    public function __construct(RendererAdapterInterface $templateRenderer)
33
    {
34
        $this->templateRenderer = $templateRenderer;
35
    }
36
37
    /**
38
     * From the request data renders an output for the response, or sets an error status code
39
     *
40
     * @param ServerRequestInterface $request
41
     * @param ResponseInterface      $response
42
     *
43
     * @return ResponseInterface
44
     */
45
    public function __invoke(ServerRequestInterface &$request, ResponseInterface $response)
46
    {
47
        $template = $request->getAttribute('template');
48
        $data = $request->getAttribute('data');
49
        /** @var StreamInterface $body */
50
        $body = $this->templateRenderer->render($template, $data);
51
        $response = $response->withBody($body);
52
53
        return $response;
54
    }
55
}
56