Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

DispatcherMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 27 5
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 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
declare(strict_types = 1);
13
14
namespace WebHemi\Middleware\Common;
15
16
use Psr\Http\Message\StreamInterface;
17
use RuntimeException;
18
use WebHemi\Http\ResponseInterface;
19
use WebHemi\Http\ServerRequestInterface;
20
use WebHemi\Middleware\MiddlewareInterface;
21
use WebHemi\Middleware\ActionMiddlewareInterface;
22
use WebHemi\Renderer\ServiceInterface as RendererInterface;
23
24
/**
25
 * Class DispatcherMiddleware.
26
 */
27
class DispatcherMiddleware implements MiddlewareInterface
28
{
29
    /** @var RendererInterface */
30
    private $templateRenderer;
31
32
    /**
33
     * DispatcherMiddleware constructor.
34
     *
35
     * @param RendererInterface $templateRenderer
36
     */
37 6
    public function __construct(RendererInterface $templateRenderer)
38
    {
39 6
        $this->templateRenderer = $templateRenderer;
40 6
    }
41
42
    /**
43
     * From the request data renders an output for the response, or sets an error status code.
44
     *
45
     * @param ServerRequestInterface $request
46
     * @param ResponseInterface      $response
47
     * @throws RuntimeException
48
     * @return void
49
     */
50 6
    public function __invoke(ServerRequestInterface&$request, ResponseInterface&$response) : void
51
    {
52
        /** @var MiddlewareInterface $actionMiddleware */
53 6
        $actionMiddleware = $request->getAttribute(ServerRequestInterface::REQUEST_ATTR_ACTION_MIDDLEWARE);
54
55
        // If there is a valid action Middleware, then dispatch it.
56 6
        if (!is_null($actionMiddleware) && $actionMiddleware instanceof ActionMiddlewareInterface) {
57
            /** @var ResponseInterface $response */
58 4
            $actionMiddleware($request, $response);
59
60
            // Create template only when there's no redirect
61 2
            if (ResponseInterface::STATUS_REDIRECT != $response->getStatusCode()) {
62
                /** @var string $template */
63 2
                $template = $request->getAttribute(ServerRequestInterface::REQUEST_ATTR_DISPATCH_TEMPLATE);
64
                /** @var array $data */
65 2
                $data = $request->getAttribute(ServerRequestInterface::REQUEST_ATTR_DISPATCH_DATA);
66
67 2
                if (!$request->isXmlHttpRequest()) {
68
                    /** @var StreamInterface $body */
69 2
                    $body = $this->templateRenderer->render($template, $data);
70 2
                    $response = $response->withBody($body);
71
                }
72
            }
73
        } else {
74 2
            throw new RuntimeException(sprintf('The given attribute is not a valid Action Middleware.'), 1000);
75
        }
76 2
    }
77
}
78