Completed
Push — master ( ef1e9b...ffc375 )
by Gabor
05:15
created

DispatcherMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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 1
    public function __construct(RendererAdapterInterface $templateRenderer)
33
    {
34 1
        $this->templateRenderer = $templateRenderer;
35 1
    }
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 1
    public function __invoke(ServerRequestInterface &$request, ResponseInterface $response)
46
    {
47
        /** @var MiddlewareInterface $actionMiddleware */
48 1
        $actionMiddleware = $request->getAttribute('actionMiddleware');
49
50
        // If there is a valid action Middleware, then dispatch it.
51 1
        if (!is_null($actionMiddleware) && $actionMiddleware instanceof MiddlewareInterface) {
52
            $response = $actionMiddleware($request, $response);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $response. This often makes code more readable.
Loading history...
53
54
            /** @var string $template */
55
            $template = $request->getAttribute('template');
56
            /** @var array $data */
57
            $data = $request->getAttribute('data');
58
            /** @var StreamInterface $body */
59
            $body = $this->templateRenderer->render($template, $data);
60
            $response = $response->withBody($body);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $response. This often makes code more readable.
Loading history...
61
        }
62
63 1
        return $response;
64
    }
65
}
66