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

DispatcherMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 58.33%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 4
c 2
b 0
f 2
lcom 0
cbo 2
dl 0
loc 44
ccs 7
cts 12
cp 0.5833
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 3
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